All Downloads are FREE. Search and download functionalities are using the official Maven repository.

sam.samlang.0.0.4.source-code.PL.g4 Maven / Gradle / Ivy

There is a newer version: 0.0.7
Show newest version
grammar PL;

import PLLexerPart;

program : module* EOF;
module : moduleHeaderDeclaration LBRACE moduleMemberDefinition* RBRACE;

// Module Level Declarations
moduleHeaderDeclaration
    : CLASS UpperId typeParametersDeclaration? LPAREN typeDeclaration RPAREN # ClassHeader
    | UTIL UpperId # UtilHeader
    ;
moduleMemberDefinition
    : PUBLIC? (FUNCTION | METHOD) typeParametersDeclaration? LowerId
        LPAREN (annotatedVariable (COMMA annotatedVariable)* COMMA?)? RPAREN (COLON typeExpr)?
      ASSIGN expression
    ;
typeParametersDeclaration : LT UpperId (COMMA UpperId)* GT;

typeDeclaration
    : objectTypeFieldDeclaration (COMMA objectTypeFieldDeclaration)* COMMA? # ObjType
    | variantTypeConstructorDeclaration (COMMA variantTypeConstructorDeclaration)+ COMMA? # VariantType
    ;
objectTypeFieldDeclaration : LowerId typeAnnotation;
variantTypeConstructorDeclaration : UpperId LPAREN typeExpr RPAREN;

// Type expressions
typeExpr
    : UNIT # UnitType
    | INT # IntType
    | STRING # StrType
    | BOOL # BoolType
    | UpperId typeParameters? # SingleIdentifierType
    | LBRACKET typeExpr (MUL typeExpr)+ RBRACKET # TupleType
    | LPAREN typeExpr (COMMA typeExpr)* COMMA? RPAREN ARROW typeExpr # FunctionType
    ;

// Some parser type fragment
typeParameters : LT typeExpr (COMMA typeExpr)* GT;
annotatedVariable : LowerId typeAnnotation;
optionallyAnnotatedParameter : LowerId typeAnnotation?;
typeAnnotation : COLON typeExpr;

patternToExpr : BAR (UpperId varOrWildCard) ARROW expression;

expression
    : LPAREN expression RPAREN # NestedExpr
    | literal # LiteralExpr
    | THIS # ThisExpr
    | LowerId # VariableExpr
    | UpperId COLONCOLON LowerId # ModuleMemberExpr
    | LBRACKET expression (COMMA expression)+ COMMA? RBRACKET # TupleConstructor
    | LBRACE (SPREAD expression COMMA)? objectFieldDeclarations RBRACE # ObjConstructor
    | UpperId LPAREN expression RPAREN # VariantConstructor
    | expression DOT LowerId # FieldAccessExpr
    | expression COLONCOLON LowerId # MethodAccessExpr
    | MINUS expression # NegExpr
    | NOT expression # NotExpr
    | PANIC LPAREN expression RPAREN # PanicExpr
    | expression functionArguments # FunctionApplicationExpr
    | expression factorOperator expression # FactorExpr
    | expression termOperator expression # TermExpr
    | expression comparisonOperator expression # ComparisonExpr
    | expression AND expression # ConjunctionExpr
    | expression OR expression # DisjunctionExpr
    | IF expression THEN expression ELSE expression # IfElseExpr
    | MATCH LPAREN expression RPAREN LBRACE patternToExpr+ RBRACE # MatchExpr
    | LPAREN optionallyAnnotatedParameter (COMMA optionallyAnnotatedParameter)* COMMA? RPAREN ARROW expression # FunExpr
    | VAL pattern typeAnnotation? ASSIGN expression SEMICOLON expression? # ValExpr
    ;

objectFieldDeclarations : objectFieldDeclaration (COMMA objectFieldDeclaration)* COMMA?;
objectFieldDeclaration
    : LowerId COLON expression # NormalObjFieldDeclaration
    | LowerId # ShorthandObjFieldDeclaration
    ;
functionArguments : LPAREN RPAREN | LPAREN expression (COMMA expression)* COMMA? RPAREN;

pattern
    : LBRACKET varOrWildCard (COMMA varOrWildCard)+ COMMA? RBRACKET # TuplePattern
    | LBRACE varOrRenamedVar (COMMA varOrRenamedVar)* COMMA? RBRACE # ObjectPattern
    | LowerId # VariablePattern
    | WILDCARD # WildcardPattern
    ;
varOrWildCard : LowerId | WILDCARD;
varOrRenamedVar
    : LowerId # RawVar
    | LowerId AS LowerId # RenamedVar
    ;

// All Operators
factorOperator : MUL | DIV | MOD;
termOperator : PLUS | MINUS;
comparisonOperator : LT | LE | GT | GE | STRUCT_EQ | STRUCT_NE;

// Literals
literal : UNIT | TRUE | FALSE | IntLiteral | StrLiteral;




© 2015 - 2024 Weber Informatics LLC | Privacy Policy