MCPcopy Create free account
hub / github.com/AutoHotkey/AutoHotkey / IsFunctionDefinition

Method IsFunctionDefinition

source/script.cpp:1613–1649  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

1611
1612
1613bool Script::IsFunctionDefinition(LPTSTR aBuf, LPTSTR aNextBuf)
1614// Helper function for LoadIncludedFile().
1615// Caller passes in an aBuf containing a candidate line such as "function(x, y)"
1616// Caller has ensured that aBuf is rtrim'd.
1617// Caller should pass NULL for aPendingFunctionHasBrace to indicate that function definitions (open-brace
1618// on same line as function) are not allowed. When non-NULL *and* aBuf is a function call/def,
1619// *aPendingFunctionHasBrace is set to true if a brace is present at the end, or false otherwise.
1620// In addition, any open-brace is removed from aBuf in this mode.
1621{
1622 LPTSTR action_end = find_identifier_end(aBuf);
1623 // Can't be a function definition or call without an open-parenthesis as first char found by the above.
1624 // action_end points at the first character which is not usable in an identifier, such as a space, tab
1625 // colon or other operator symbol. As a result, it can't be:
1626 // 1) a hotstring, since they always start with at least one colon that would be caught immediately as
1627 // first-expr-char-is-not-open-parenthesis by the above.
1628 // 2) Any kind of math or assignment, such as var:=(x+y) or var+=(x+y).
1629 // The only things it could be other than a function call or function definition are:
1630 // Normal label that ends in single colon but contains an open-parenthesis prior to the colon, e.g. Label(x):
1631 // Single-line hotkey such as KeyName::MsgBox. But (:: is the only valid hotkey where *action_end == '(',
1632 // and that's handled by excluding action_end == aBuf.
1633 if (*action_end != '(' || action_end == aBuf)
1634 return false;
1635 // Is it a control flow statement, such as "if(condition)"?
1636 *action_end = '\0';
1637 bool is_control_flow = ConvertActionType(aBuf);
1638 *action_end = '(';
1639 if (is_control_flow)
1640 return false;
1641 // It's not control flow.
1642 LPTSTR param_end = action_end + FindExprDelim(action_end, ')', 1);
1643 if (*param_end != ')')
1644 return false;
1645 LPTSTR next_token = omit_leading_whitespace(param_end + 1);
1646 return *next_token == 0 && *aNextBuf == '{' // Brace on next line.
1647 || *next_token == '{' && next_token[1] == 0 // Brace on same line.
1648 || *next_token == '=' && next_token[1] == '>'; // Fn() => expr
1649}
1650
1651
1652

Callers

nothing calls this directly

Calls 3

find_identifier_endFunction · 0.85
FindExprDelimFunction · 0.85
omit_leading_whitespaceFunction · 0.85

Tested by

no test coverage detected