Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Некорректная грамматика #14

Closed
Stepami opened this issue Sep 19, 2022 · 1 comment · Fixed by #28
Closed

Некорректная грамматика #14

Stepami opened this issue Sep 19, 2022 · 1 comment · Fixed by #28
Labels
bug Something isn't working

Comments

@Stepami
Copy link
Owner

Stepami commented Sep 19, 2022

На данный момент, в грамматике присутствуют серьёзные ошибки приоритета операций в части выражений (expressions).

Например, при работе с полями объекта, доступ через . надо оборачивать в скобки для верного приоритета:

let xy = this.x + this.y // не спарсится корректно

// ...

let xy = (this.x) + (this.y) // корректно парсится, но приходится извращаться

Необходимо переработать грамматику, а затем, соответственно, парсер + структуру и иерархию узлов AST

@Stepami
Copy link
Owner Author

Stepami commented Jan 18, 2023

Script -> StatementList
StatementList -> StatementListItem*
StatementListItem -> Statement
                     Declaration
                     
Statement -> BlockStatement
             ExpressionStatement
             IfStatement
             WhileStatement
             ContinueStatement
             BreakStatement
             ReturnStatement
             TypeStatement
             
Declaration -> LexicalDeclaration
               FunctionDeclaration
               
BlockStatement -> '{' StatementList '}'

ExpressionStatement -> Expression
Expression -> CastExpression   
              AssignmentExpression

CastExpression -> ConditionalExpression 'as' 'string'

AssignmentExpression -> LeftHandSideExpression ('=' Expression)?

LeftHandSideExpression -> MemberExpression
			  CallExpression
CallExpression -> MemberExpression Arguments (Arguments | '[' Expression ']' | '.' 'Ident')*
MemberExpression -> PrimaryExpression ('[' Expression ']' | '.' 'Ident')*

Arguments -> '(' (Expression ',')* ')'

ConditionalExpression -> OrExpression ('?' Expression ':' Expression)?
OrExpression -> AndExpression ('||' AndExpression)*
AndExpression -> EqExpression ('&&' EqExpression)*
EqExpression -> RelExpression (('=='|'!=') RelExpression)*
RelExpression -> AddExpression (('<'|'>'|'<='|'>=') AddExpression)*
AddExpression -> MulExpression (('+'|'-') MulExpression)*
MulExpression -> UnaryExpression (('*'|'/'|'%'|'++'|'::') UnaryExpression)*
UnaryExpression -> LeftHandSideExpression | ('-'|'!'|'~') UnaryExpression

PrimaryExpression -> "Ident" | Literal | '(' Expression ')' | ObjectLiteral | ArrayLiteral | 'this'
Literal -> "NullLiteral"
           "IntegerLiteral"
           "FloatLiteral"
           "StringLiteral"
           "BooleanLiteral"
ObjectLiteral -> '{' PropertyDefinitionList '}'
PropertyDefinitionList -> (Property ';')*
Property -> FieldProperty
            MethodProperty
FieldProperty -> "Ident" ':' Expression
MethodProperty -> "Ident" '=>' MethodDeclaration
MethodDeclaration -> '(' FunctionParameters? ')' Type? BlockStatement
ArrayLiteral -> '[' ElementList ']'
ElementList -> (Expression ',')*
             
IfStatement -> 'if' '(' Expression ')' Statement ('else' Statement)?

WhileStatement -> 'while' '(' Expression ')' Statement

ContinueStatement -> 'continue'

BreakStatement -> 'break'

ReturnStatement -> 'return' Expression?

TypeStatement -> 'type' "Ident" = TypeValue
TypeValue -> TypeValueBase TypeValueSuffix*
TypeValueBase -> "Ident"
                 FunctionTypeBase
                 ObjectTypeBase
ObjectTypeBase -> '{' PropertyTypeList '}'
PropertyTypeList -> (PropertyType ';')*
PropertyType -> "Ident" ':' TypeValue
FunctionTypeBase -> '(' ArgTypeList ')' '=>' TypeValue
ArgTypeList -> (TypeValue ',')*
TypeValueSuffix -> '['']'
                   '?'
 
LexicalDeclaration -> LetOrConst "Ident" Initialization (',' "Ident" Initialization)*
LetOrConst -> 'let'
              'const'
Initialization -> Typed
                  Initializer
Typed -> Type Initializer?
Initializer -> '=' Expression

FunctionDeclaration -> 'function' "Ident" '(' FunctionParameters? ')' Type? BlockStatement
FunctionParameters -> ParameterDeclaration (',' ParameterDeclaration)*
ParameterDeclaration -> "Ident" Type
Type -> ':' TypeValue

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant