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

make some parts of the grammar easier to read and more DRY, format with the default Langium formatter #2

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
85 changes: 44 additions & 41 deletions src/language-server/lox.langium
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ grammar Lox
entry LoxProgram:
elements+=LoxElement*;

LoxElement:
LoxElement:
Class |
ExpressionBlock |
IfStatement |
Expand All @@ -16,7 +16,7 @@ LoxElement:
Expression ';'
;

IfStatement:
IfStatement:
'if' '(' condition=Expression ')' block=ExpressionBlock
('else' elseBlock=ExpressionBlock)?
;
Expand All @@ -29,13 +29,14 @@ ForStatement:
'for' '(' counter=VariableDeclaration? ';' condition=Expression? ';' execution=Expression? ')' block=ExpressionBlock
;

PrintStatement: 'print' value=Expression;
PrintStatement:
'print' value=Expression;

ReturnStatement: 'return' value=Expression?;
ReturnStatement:
'return' value=Expression?;

ExpressionBlock: '{'
elements+=LoxElement*
'}';
ExpressionBlock:
'{' elements+=LoxElement* '}';

VariableDeclaration returns NamedElement:
{infer VariableDeclaration} 'var' name=ID (':' type=TypeReference)? (assignment?='=' value=Expression)?
Expand All @@ -61,23 +62,14 @@ Comparison infers Expression:

MemberCall infers Expression:
Primary
({infer MemberCall.previous=current}
// Member call with function call
("." element=[NamedElement:ID] (
explicitOperationCall?='('
(
arguments+=Expression (',' arguments+=Expression)*
)?
')')?
// Chained function call
| (
explicitOperationCall?='('
(
arguments+=Expression (',' arguments+=Expression)*
)?
')'))
// Member call with function call
({infer MemberCall.previous=current}
"." element=[NamedElement:ID] ( FunctionCallArguments )?
// Chained function call
| ( FunctionCallArguments )
)*;


Primary infers Expression:
'(' Expression ')' |
UnaryExpression |
Expand All @@ -88,46 +80,57 @@ Primary infers Expression:
FeatureCall;

FeatureCall infers Expression:
{infer MemberCall}
(element=[NamedElement:ID] | element=[NamedElement:'this'] | element=[NamedElement:'super'])
{infer MemberCall}
( element=[NamedElement:ID] | element=[NamedElement:'this'] | element=[NamedElement:'super'] )
// Optional function call after referencing an element
(explicitOperationCall?='('
(
arguments+=Expression (',' arguments+=Expression)*
)?
')')?;
( FunctionCallArguments )?;

fragment FunctionCallArguments:
explicitOperationCall?='(' ( arguments+=Expression (',' arguments+=Expression)* )? ')';

UnaryExpression:
operator=('!' | '-' | '+') value=Expression
operator=('!' | '-' | '+') value=Expression
;

NumberExpression: value=NUMBER;
StringExpression: value=STRING;
BooleanExpression: value?='true' | 'false';
NilExpression: value='nil';
NumberExpression:
value=NUMBER;
StringExpression:
value=STRING;
BooleanExpression:
value?='true' | 'false';
NilExpression:
value='nil';

FunctionDeclaration:
'fun' name=ID '(' (parameters+=Parameter (',' parameters+=Parameter)*)? ')' ':' returnType=TypeReference body=ExpressionBlock;
'fun' name=ID ( ParameterList ) ':' returnType=TypeReference body=ExpressionBlock;

Parameter: name=ID ':' type=TypeReference;
fragment ParameterList:
'(' (parameters+=Parameter (',' parameters+=Parameter)*)? ')';

Parameter:
name=ID ':' type=TypeReference;

Class: 'class' name=ID ('<' superClass=[Class:ID])? '{'
Class:
'class' name=ID ('<' superClass=[Class:ID])? '{'
members+=ClassMember*
'}';

ClassMember: MethodMember | FieldMember;
ClassMember:
MethodMember | FieldMember;

MethodMember:
name=ID '(' (parameters+=Parameter (',' parameters+=Parameter)*)? ')' ':' returnType=TypeReference body=ExpressionBlock;
name=ID ( ParameterList ) ':' returnType=TypeReference body=ExpressionBlock;

FieldMember:
name=ID ':' type=TypeReference;

TypeReference: reference=[Class:ID]
TypeReference:
reference=[Class:ID]
| primitive=("string" | "number" | "boolean" | "void")
| '(' ( parameters+=LambdaParameter (',' parameters+=LambdaParameter)*)? ')' '=>' returnType=TypeReference;

LambdaParameter: (name=ID ':')? type=TypeReference;
LambdaParameter:
(name=ID ':')? type=TypeReference;

type NamedElement = Parameter | FunctionDeclaration | VariableDeclaration | MethodMember | FieldMember | Class;

Expand Down