polserver/lib/Parser/EscriptGrammar/EscriptParser.g4

468 lines
9.4 KiB
Text
Raw Permalink Normal View History

parser grammar EscriptParser;
options { tokenVocab=EscriptLexer; }
@header
{
}
@parser::members
{
}
compilationUnit
: topLevelDeclaration* EOF
;
moduleUnit
: moduleDeclarationStatement* EOF
;
Introduce DAP Debugger (#608) * Add cppdap debugger library and simple server (#508) * Add cppdap library * Add cppdap server * fixup cmake scripts for windows * Enable caching of cppdap on GitHub actions * Fix CMake for cppdap target * Wait 1s for session close before thread ends * Address review comments - Remove unnecessary use of wait_for - Fix wait_for method * Do not use SocketClientThread for cppdap clients * Address review comments - Cleanup object ownership * Add authorization and event handling (#510) * Add authorization and event handling [WIP] * Use std::weak_ptr; Only allow one debugger conn * Only attach debugger if dbg file exists * Cleanup weak_ptr; Fix listeners; Add handler stubs * Address review comments - PolLock on uoexec and weakptr access - Use make_shared when creating client thread * Add ExecutorDebugListener on_destroy callback * Use cppdap sockets instead of clib sockets * Update cppdap repository to 2703cf4 * Implement server address binding * Update cppdap repository to eeca5ce * Fix warnings * Use weak_from_this * Fix stalled executors on disconnect * Fix stalled executors on re-attach * Clear uoexec weakptr on all debugger detaches * Move server to gamestate * move DapDebugServer into Network namespace * move DapDebugServer into GameState for storage * Move DapDebugServer into NetworkManager * Fix missing memory include * Add more events (#513) * Add more events * Implement dbg_step_out * Fix breakpoint clearing * Add even more events (#515) * add launch request * Add custom `processes` command * fixup loop * Refactoring to move handlers to member functions * Add VariablesRequest and ScopesRequest * use std::varaint for Handles * add pollocks * refactor Handles to new file * move custom messages to proto.h * move clientthread * some formatting / cleanup * Add missing PolLock * move dap variables creation to Handles * appobj members * rename ClientThread to DebugClientThread * rename LaunchRequest args to arg * Fix compiler error on size_t * Add support for EvaluateRequest and SetVariableRequest (#519) * Update grammar for evaluateUnit * Cleanup various compiler code * Add support for EvaluateRequest Only supports values, variables, element access, member access * Add report clearing to reinitialize state * Add support for SetVariableRequest * Fix CI build errors * Move ExpressionEvaluator to Bscript * Fix formatting * Fix include * Refactor global and frame variable references * Encapsulate Executor debugging environment; Fix step over (#524) * Fix step over * Some cleanup of executor - Remove unused debug states - Rename members for consistency * move debugging environment to new class * Move instruction inspection to class * Cleanup slashes on Windows; Add Executor state to processes response (#527) * Fix backspaces in script names on Windows * Fix compilation warnings with data loss * Use boost::icontains for string comparison * Add Executor state to processes response * Updates for libfmt * Fix tests; Fix cppdap build for Mac universal binary (#607) * fix tests * fix cppdap build for mac universal binary * Update cppdap to 2a4c7cf * Add tests; Modify launch behavior to not stop at entry (redo) (#609) * Remove unused setExceptionBreakpoints handler * Add debugger tests * Launching a script should not set attaching state * Add launch tests * Do not check for connection in debugger tests * Rename `program` to `script` in protocol structs * Fix launch stopAtEntry; quote string var values * Fix compilation error on Windows * Add configfiles doc entry * Various fixes for finishing touches; Add core-changes (#610) * switch cmake projects * simple docs * cmake move make_directory * Fix launch request with relative, absolute, package paths * make relative paths as absolute in source request * Add true, false, uninit to ExpressionEvaluator * Re-add setExceptionBreakpoints to avoid unhandled message errors * Better debug thread logging with instance numbers * launched scripts should terminate on disconnect (according to DAP spec) * Properly handle exited event in client thread * Add core-changes * Address Discord comments - Mention vscode-escript in core-changes - Re-add "cppdap already built" in cmake * change cmake escriptgrammarlib visibility to public
2024-01-28 15:49:55 +07:00
evaluateUnit
: expression EOF
;
moduleDeclarationStatement
: moduleFunctionDeclaration
| constStatement
;
moduleFunctionDeclaration
: IDENTIFIER '(' moduleFunctionParameterList? ')' ';'
;
moduleFunctionParameterList
: moduleFunctionParameter (',' moduleFunctionParameter)*
;
moduleFunctionParameter
: IDENTIFIER (':=' expression)?
;
topLevelDeclaration
: useDeclaration
| includeDeclaration
| programDeclaration
| functionDeclaration
| classDeclaration
| statement
;
classDeclaration
: CLASS IDENTIFIER classParameters classBody ENDCLASS
;
classParameters
: '(' classParameterList? ')'
;
classParameterList
: IDENTIFIER (',' IDENTIFIER)*
;
classBody
: classStatement*
;
classStatement
: functionDeclaration
| varStatement
;
functionDeclaration
: EXPORTED? FUNCTION IDENTIFIER functionParameters block ENDFUNCTION
;
stringIdentifier
: STRING_LITERAL
| IDENTIFIER
;
useDeclaration
: USE stringIdentifier ';'
;
includeDeclaration
: INCLUDE stringIdentifier ';'
;
programDeclaration
: PROGRAM IDENTIFIER programParameters block ENDPROGRAM
;
// Some ignored / to-be-handled things:
// - Labels can only come before DO, WHILE, FOR, FOREACH, REPEAT, and CASE statements.
// - Const expression must be optimizable
// TODO maybe split these all into individual statements?
statement
: ifStatement
| gotoStatement
| returnStatement
| constStatement
| varStatement
| doStatement
| whileStatement
| exitStatement
| breakStatement
| continueStatement
| forStatement
| foreachStatement
| repeatStatement
| caseStatement
| enumStatement
| SEMI
| statementExpression=expression ';'
;
statementLabel
: IDENTIFIER ':'
;
ifStatement
: IF parExpression THEN? block (ELSEIF parExpression block)* (ELSE block)? ENDIF
;
gotoStatement
: GOTO IDENTIFIER ';'
;
returnStatement
: RETURN expression? ';'
;
constStatement
: TOK_CONST constantDeclaration ';'
;
varStatement
: VAR variableDeclarationList ';'
;
doStatement
: statementLabel? DO block DOWHILE parExpression ';'
;
whileStatement
: statementLabel? WHILE parExpression block ENDWHILE
;
exitStatement
: EXIT ';'
;
breakStatement
: BREAK IDENTIFIER? ';'
;
continueStatement
: CONTINUE IDENTIFIER? ';'
;
forStatement
: statementLabel? FOR forGroup ENDFOR
;
foreachIterableExpression
: functionCall
| scopedFunctionCall
| scopedIdentifier
| IDENTIFIER
| parExpression
| bareArrayInitializer
| explicitArrayInitializer
;
foreachStatement
: statementLabel? FOREACH IDENTIFIER TOK_IN foreachIterableExpression block ENDFOREACH
;
repeatStatement
: statementLabel? REPEAT block UNTIL expression ';'
;
caseStatement
: statementLabel? CASE '(' expression ')' switchBlockStatementGroup+ ENDCASE
;
enumStatement
: ENUM IDENTIFIER enumList ENDENUM
;
block
: statement*
;
variableDeclarationInitializer
: ':=' expression
| '=' expression { notifyErrorListeners("Unexpected token: '='. Did you mean := for assign?\n"); }
| ARRAY
;
enumList
: enumListEntry (',' enumListEntry)* ','?
;
enumListEntry
: IDENTIFIER (':=' expression)?
;
switchBlockStatementGroup
: switchLabel+ block
;
switchLabel
: (integerLiteral | boolLiteral | UNINIT | IDENTIFIER | STRING_LITERAL) ':'
| DEFAULT ':'
;
forGroup
: cstyleForStatement
| basicForStatement
;
basicForStatement
: IDENTIFIER ':=' expression TO expression block
;
cstyleForStatement
: '(' expression ';' expression ';' expression ')' block
;
identifierList
: IDENTIFIER (',' identifierList)?
;
variableDeclarationList
: variableDeclaration (',' variableDeclaration)*
;
constantDeclaration
: IDENTIFIER variableDeclarationInitializer
;
variableDeclaration
: IDENTIFIER variableDeclarationInitializer?
;
// PARAMETERS
programParameters
: '(' programParameterList? ')'
;
programParameterList
: programParameter (','? programParameter)*
;
programParameter
: UNUSED IDENTIFIER
| IDENTIFIER (':=' expression)?
;
functionParameters
: '(' functionParameterList? ')'
;
functionParameterList
: functionParameter (',' functionParameter)*
;
functionParameter
: BYREF? UNUSED? IDENTIFIER ELLIPSIS? (':=' expression)?
;
// EXPRESSIONS
//
// Currently no module functions return a function reference, so no need to
// support eg. `uo::Foo( bar )( baz )`.
scopedFunctionCall
: IDENTIFIER? '::' functionCall
;
functionReference
: '@' (scope=IDENTIFIER? '::')? function=IDENTIFIER
;
expression
: primary
| expression expressionSuffix
| expression postfix=('++' | '--')
| prefix=('+'|'-'|'++'|'--') expression
| prefix=('~'|'!'|'not') expression
| expression bop=('*' | '/' | '%' | '<<' | '>>' | '&') expression
| expression bop=('+' | '-' | '|' | '^') expression
| expression bop='?:' expression
| expression bop='in' expression
| expression bop=('<=' | '>=' | '>' | '<') expression
| expression bop='is' expression
| expression bop='=' { notifyErrorListeners("Deprecated '=' found: did you mean '==' or ':='?\n"); } expression
| expression bop=('==' | '!=' | '<>') expression
| expression bop=('&&' | 'and') expression
| expression bop=('||' | 'or') expression
| expression '?' expression ':' expression
| <assoc=right> expression bop=('.+' | '.-' | '.?') expression
| <assoc=right> expression
bop=( ':=' | '+=' | '-=' | '*=' | '/=' | '%=')
expression
;
primary
: literal
| parExpression
| functionCall
| scopedFunctionCall
| scopedIdentifier
| IDENTIFIER
| functionReference
Add compiler support for function expressions (#671) * initial poc of function expressions - update grammar - mock AST builder to return BBoolean(true) for a func expr - update prettifier for skeleton implementation * more skeleton work create AST class FunctionExpression mimicking boolean value * create ast UserFunction, add to workspace from functexpr * can generate instructions * reorg tests; add test for instructions * can track FunctionDepth in Variable * can get captures for funcexprs inside funcs.. tbd if this way of nesting works * Implement create-functor instruction - Move function depth from Variable to Variables - Introduce stacking of `Variables` for function expresions via `FunctionVariableScope` - Add `TOK_FUNCTOR` instruction for 'create-functor' - Handle emitting a `FunctionExpression` AST node - Update `emit.declare_variable` and `emit.access_variable` to account for function captures - Remove `in_function` from instruction generator, as it is tracked via `UserFunction` stack - Update tests * Address Discord comments - Swap pop param order * Bubble up captured variables through nested functions * Improve testing infrastructure; add some test cases * Fix compilation error * prepend captures to function parameters in funcref mth_call * update tests * move from function{} to @{} * implementation fix * some more tests * update docs * Some cleanup * fix CI annotation warning - 'argument': conversion from 'size_t' to 'VariableIndex', possible loss of data * Address review comments - Add `passert_always` - Use better example in docs * Some cleanup - Remove unused functions
2024-07-29 22:30:52 +02:00
| functionExpression
| explicitArrayInitializer
| explicitStructInitializer
| explicitDictInitializer
| explicitErrorInitializer
| bareArrayInitializer
Add support for interpolated strings ("interstrings") (#369) * Update grammar to support interpolated strings * Struct initializers, case switchs are regular strings * Initial stub for interpolated string in AST This splits the previous STRING_LITERAL terminal into a production, stringLiteral, to handle REGULAR_STRING terminal and interpolatedString production. * Change interpolated strings to expressions * Fix mode handling in grammar * Grammar fixes - Fix mode handling in lexer - Interpolated strings only have one expression * Can successfully parse, stub compile interstrings $"He {there + print("hello")}"; * Add formatting string to grammar * Fix tests, and add test stubs * Rename curleys to brace; better AST generation * initial work interstring instruction * Initial work on formatted string instruction: emit * Formatted string instruction: execution * Add simple formatted string test * Add formatted string test src * Review changes #1 - Rename `FormattedString` to `FormatExpression` - Rename overloaded `try_to_format()` to `get_formatted()` * Review changes part 2 - Rename `InterpolatedString` ast node to `InterpolateString` * Review changes part 3 - Modify `ins_interpolate_string` * Review changes part 4 - Remove unused formal parameter * Add negative tests * Spruce up positive tests * Fix braces, escape chars inside interstring * Fix, add tests for escaped char and double brace * Address differences for new lines in tests * Update Escript version * Make empty expression an error * Add documentation for interpolated strings * Quick touchup on docs
2021-03-11 22:48:16 +01:00
| interpolatedString
;
scopedIdentifier
: scope=IDENTIFIER? '::' identifier=IDENTIFIER;
Add compiler support for function expressions (#671) * initial poc of function expressions - update grammar - mock AST builder to return BBoolean(true) for a func expr - update prettifier for skeleton implementation * more skeleton work create AST class FunctionExpression mimicking boolean value * create ast UserFunction, add to workspace from functexpr * can generate instructions * reorg tests; add test for instructions * can track FunctionDepth in Variable * can get captures for funcexprs inside funcs.. tbd if this way of nesting works * Implement create-functor instruction - Move function depth from Variable to Variables - Introduce stacking of `Variables` for function expresions via `FunctionVariableScope` - Add `TOK_FUNCTOR` instruction for 'create-functor' - Handle emitting a `FunctionExpression` AST node - Update `emit.declare_variable` and `emit.access_variable` to account for function captures - Remove `in_function` from instruction generator, as it is tracked via `UserFunction` stack - Update tests * Address Discord comments - Swap pop param order * Bubble up captured variables through nested functions * Improve testing infrastructure; add some test cases * Fix compilation error * prepend captures to function parameters in funcref mth_call * update tests * move from function{} to @{} * implementation fix * some more tests * update docs * Some cleanup * fix CI annotation warning - 'argument': conversion from 'size_t' to 'VariableIndex', possible loss of data * Address review comments - Add `passert_always` - Use better example in docs * Some cleanup - Remove unused functions
2024-07-29 22:30:52 +02:00
functionExpression
: AT functionParameters? LBRACE block RBRACE
;
explicitArrayInitializer
: ARRAY arrayInitializer?
;
explicitStructInitializer
: STRUCT structInitializer?
;
explicitDictInitializer
: DICTIONARY dictInitializer?
;
explicitErrorInitializer
: TOK_ERROR structInitializer?
;
bareArrayInitializer
: LBRACE expressionList? RBRACE
| LBRACE expressionList? ',' RBRACE {notifyErrorListeners("Expected expression following comma before right-brace in array initializer list");}
;
parExpression
: '(' expression ')'
;
expressionList
: expressionListEntry (',' expressionListEntry)*
;
expressionListEntry
: expression ELLIPSIS?
;
expressionSuffix
: indexingSuffix
| methodCallSuffix
| navigationSuffix
| functionCallSuffix
;
indexingSuffix
: LBRACK indexList RBRACK
;
indexList
: expression (',' expression)*
;
navigationSuffix
: '.' ( IDENTIFIER | STRING_LITERAL | FUNCTION )
;
methodCallSuffix
: '.' IDENTIFIER LPAREN expressionList? RPAREN
;
functionCallSuffix
: '(' expressionList? ')'
;
functionCall
: IDENTIFIER '(' expressionList? ')'
;
structInitializerExpression
: IDENTIFIER (':=' expression)?
| STRING_LITERAL (':=' expression)?
;
structInitializerExpressionList
: structInitializerExpression (',' structInitializerExpression)*
;
structInitializer
: '{' structInitializerExpressionList? '}'
| '{' structInitializerExpressionList? ',' '}' {notifyErrorListeners("Expected expression following comma before right-brace in struct initializer list");}
;
dictInitializerExpression
: expression ('->' expression)?
;
dictInitializerExpressionList
: dictInitializerExpression (',' dictInitializerExpression)*
;
dictInitializer
: '{' dictInitializerExpressionList? '}'
| '{' dictInitializerExpressionList? ',' '}' {notifyErrorListeners("Expected expression following comma before right-brace in dictionary initializer list");}
;
arrayInitializer
: '{' expressionList? '}'
| '{' expressionList? ',' '}' {notifyErrorListeners("Expected expression following comma before right-brace in array initializer list");}
| '(' expressionList? ')'
| '(' expressionList? ',' ')' {notifyErrorListeners("Expected expression following comma before right-paren in array initializer list");}
;
// Literals
literal
: integerLiteral
| floatLiteral
| boolLiteral
| STRING_LITERAL
| UNINIT
;
Add support for interpolated strings ("interstrings") (#369) * Update grammar to support interpolated strings * Struct initializers, case switchs are regular strings * Initial stub for interpolated string in AST This splits the previous STRING_LITERAL terminal into a production, stringLiteral, to handle REGULAR_STRING terminal and interpolatedString production. * Change interpolated strings to expressions * Fix mode handling in grammar * Grammar fixes - Fix mode handling in lexer - Interpolated strings only have one expression * Can successfully parse, stub compile interstrings $"He {there + print("hello")}"; * Add formatting string to grammar * Fix tests, and add test stubs * Rename curleys to brace; better AST generation * initial work interstring instruction * Initial work on formatted string instruction: emit * Formatted string instruction: execution * Add simple formatted string test * Add formatted string test src * Review changes #1 - Rename `FormattedString` to `FormatExpression` - Rename overloaded `try_to_format()` to `get_formatted()` * Review changes part 2 - Rename `InterpolatedString` ast node to `InterpolateString` * Review changes part 3 - Modify `ins_interpolate_string` * Review changes part 4 - Remove unused formal parameter * Add negative tests * Spruce up positive tests * Fix braces, escape chars inside interstring * Fix, add tests for escaped char and double brace * Address differences for new lines in tests * Update Escript version * Make empty expression an error * Add documentation for interpolated strings * Quick touchup on docs
2021-03-11 22:48:16 +01:00
interpolatedString
: INTERPOLATED_STRING_START interpolatedStringPart* DOUBLE_QUOTE_INSIDE
;
interpolatedStringPart
: LBRACE_INSIDE expression (':' FORMAT_STRING)?
| LBRACE_INSIDE (':' FORMAT_STRING)? {notifyErrorListeners("Expected expression following interpolated string part start");}
| DOUBLE_LBRACE_INSIDE
| REGULAR_CHAR_INSIDE
| DOUBLE_RBRACE
| STRING_LITERAL_INSIDE
;
integerLiteral
: DECIMAL_LITERAL
| HEX_LITERAL
| OCT_LITERAL
| BINARY_LITERAL
;
floatLiteral
: FLOAT_LITERAL
| HEX_FLOAT_LITERAL
;
boolLiteral
: BOOL_TRUE
| BOOL_FALSE
;