TO DO: Complete

NOTE: Attributes and comments right before the start of a definition are associated with that definition. However, if the comments or attributes are in the middle, they may be lost, or assigned to the next construction that is associated with attributes...

Pure attributes are supposed to be just before definitions... However, comments are also assigned to the program, and their behavior has to be better designed. Currently it is not too consistent. :(

Implements

Constructors

Methods

Auxiliaries for parsing constructions

Implementation: Auxiliaries

Implementation: Parsing constructions

Implementation: Parsing constructions (Auxiliary elements)

Implementation: Parsing constructions (Definitions)

Implementation: Parsing constructions (Expressions)

Implementation: Parsing constructions (Parts)

Implementation: Parsing constructions (Statements)

Implementation: Properties -- State

Constructors

Methods

  • TO DO: Complete

    Parameters

    • input: SourceInput

    Returns AST

Auxiliaries for parsing constructions

  • Private

    Return the concrete element corresponding to the given token tag, depending of the concrete word in the language words definition.

    Parameters

    • tag: symbol

    Returns GErrorElement

  • Private

    Return the token corresponding to the function name for the given operator, using for it the name of the abstract word for the operator.

    PRECONDITION:

    • the operator op is one of the operators at the given level

    Parameters

    Returns Token

Implementation: Auxiliaries

  • Private

    Confirm that the current token has the expected tag, or fail if it is not. Its use is to fail with a controlled error in case the precondition of this._match is not satisfied. So, it usually will appear before the use of this._match with the same expectedTokenTag. Consider also using _matchTokenInConstruction for a combination of both.

    PRECONDITION:

    • the current token tag is the same as the one expected

    Parameters

    • expectedTokenTag: symbol
    • construction: string
    • Optional name: string

    Returns void

  • Private

    TO DO: Complete

    ** PRECONDITION:**

    • this._lexer.hasNextGToken()

    THE TRY-CATCH HAS TO BE REVISITED AND THOUGHT WITH MORE PRECISION NOTE: the precondition is tested because this operations is meant to be used a lot during parsing, and it will be cumbersome to test on each call for its error. If some token is expected, but there are no more of them, a Parser error should be thrown -- not a Lexer error.

    Returns void

    Throws

    GErrors.NoMoreInputErrorIn is there are no more GTokens

  • Private

    Confirm that the current token has the expected tag, or fail if it is not. If the tag is correct, advance to the next token, if there is one. It is not supposed to be used to match EOD.

    PRECONDITION: (not verified)

    • the current token tag is the same as the one expected
    • the expected token tag is a SymbolicKeyword or a Keyword
    • the expected token tag is NOT EOD (so, there is no need to check for hasNextGToken)

    Parameters

    • expectedTokenTag: symbol

    Returns void

  • Private

    Combines the actions of _expectTokenInConstruction and _match, for the cases where they must be used one after the other.

    PRECONDITION:

    • the current token tag is the same as the one expected
    • the expected token tag is a SymbolicKeyword or a Keyword
    • the expected token tag is NOT EOD (so, there is no need to check for hasNextGToken)

    Parameters

    • expectedTokenTag: symbol
    • construction: string
    • Optional name: string

    Returns void

  • Private

    Match the current GToken if its tag is the one given, or do nothing if it is not. It implements the possibility to have an optional token. It is not supposed to be used to match EOD.

    PRECONDITION:

    • the expected token tag is NOT EOD (so, there is no need to check for hasNextGToken)

    Parameters

    • expectedTokenTag: symbol

    Returns void

  • Private

    Return the next token, if it is the one expected, or fail if it is not. If the tag is correct, advance to the next token, if there is one. It is not supposed to be used to expect for EOD.

    PRECONDITION:

    • the current token tag is the same as the one expected
    • the expected token tag is NOT EOD (so, there is no need to check for hasNextGToken)

    Parameters

    • expectedTokenTag: symbol

    Returns Token

  • Private

    Load the next token if there is one, when the current token is an EOD. It does nothing if the current token is not an EOD, or if there are no more tokens.

    ** PRECONDITION:**

    • this._currentGToken has EOD tag

    Returns void

Implementation: Parsing constructions

  • Private

    Parse the sequence of Definitions forming the source code, if there is a well formed one starting at the current GToken. It fails if current GToken does not start the described element, or if it or any of its parts are not well formed.

    Grammar rule: := *

    ** PRECOND:**

    • the current GToken is one starting a Definition or its tag is EOD
    • the described element starting here and all its parts are well formed

    Returns AST

  • Private

    Parse a Definition, if there is a well formed one starting at the current GToken. It fails if current GToken does not start the described element, or if it or any of its parts are not well formed. It is not supposed to be used when the current GToken is EOD.

    Grammar rule: := | | | |

    ** PRECOND:**

    • the current GToken is one of those starting a Definition
    • the described element starting here and all its parts are well formed
    • the current GToken tag is not EOD, even when the previous preconditions does not hold

    Returns ASTDef

  • Private

    Parses an Expression at the given fixity level, if there is a well formed one starting at the curren GToken. It fails if current GToken does not start the described element, if it or any of its parts are not well formed, if the fixity of the level is not correct in the language declaration, or if the fixity of the level is not respected.

    Grammar rule: (level) := (level) | (level) | (level) | (level) |

    PRECONDITION:

    • the current GToken is one of those starting an Expression
    • the described element starting here and all its parts are well formed
    • the fixities of operator levels declared in this._langWords are in BaseFixities
    • the fixity of the level is respected

    Parameters

    • level: number

    Returns ASTExpr

  • Private

    Parses an Expression at non-associative fixity level, if there is a well formed one starting at the curren GToken. It fails if current GToken does not start the described element, if it or any of its parts are not well formed, or if the fixity of the level is not respected.

    Grammar rule: (level) := (level + 1) (level) (level + 1) | (level + 1)

    PRECONDITION:

    • the current GToken is one of those starting an Expression
    • the described element starting here and all its parts are well formed
    • the fixity of the level is respected

    Parameters

    • level: number

    Returns ASTExpr

  • Private

    Parses an Expression at a left associative fixity level, if there is a well formed one starting at the curren GToken. It fails if current GToken does not start the described element, if it or any of its parts are not well formed, or if the fixity of the level is not respected.

    Grammar rule: (level) := (level + 1) ((level) (level + 1))*

    PRECONDITION:

    • the current GToken is one of those starting an Expression
    • the described element starting here and all its parts are well formed
    • the fixity of the level is respected

    Parameters

    • level: number

    Returns ASTExpr

  • Private

    Parses an Expression at a right associative fixity level, if there is a well formed one starting at the curren GToken. It fails if current GToken does not start the described element, if it or any of its parts are not well formed, or if the fixity of the level is not respected.

    Grammar rule: (level) := (level + 1) ((level) (level))*

    PRECONDITION:

    • the current GToken is one of those starting an Expression
    • the described element starting here and all its parts are well formed
    • the fixity of the level is respected

    Parameters

    • level: number

    Returns ASTExpr

  • Private

    Parses an Expression at a prefix fixity level, if there is a well formed one starting at the curren GToken. It fails if current GToken does not start the described element, if it or any of its parts are not well formed, or if the fixity of the level is not respected.

    Grammar rule: (level) := (level) (level) | (level + 1)

    PRECONDITION:

    • the current GToken is one of those starting an Expression
    • the described element starting here and all its parts are well formed
    • the fixity of the level is respected

    Parameters

    • level: number

    Returns ASTExpr

  • Private

    Parses an Expression at top level, if there is a well formed one starting at the curren GToken. It fails if current GToken does not start the described element, or if it or any of its parts are not well formed.

    Grammar rule: := (0)

    PRECONDITION:

    • the current GToken is one of those starting an Expression
    • the described element starting here and all its parts are well formed

    Returns ASTExpr

  • Private

    Parse a Statment, if there is a well formed one starting at the current GToken. It fails if current GToken does not start the described element, or if it or any of its parts are not well formed.

    Grammar rule: := | | | | | | | | | |

    PRECONDITION:

    • the current GToken is one of those starting a Statement
    • the described element starting here and all its parts are well formed

    Returns ASTStmt

Implementation: Parsing constructions (Auxiliary elements)

  • Private

    Parse the argument list of a procedure or function definition, if there is a well formed one starting at the current GToken. It fails if current GToken does not start the described element, or if it or any of its parts are not well formed.

    Grammar rule: --> ( (COMMA )*)?

    ** PRECOND:**

    • the described element starting here and all its parts are well formed

    Parameters

    • construction: string
    • name: string

    Returns ASTExpr[]

  • Private

    Parse the remainder of a procedure or function definition, if there is a well formed one starting at the current GToken. It fails if current GToken does not start the described element, or if it or any of its parts are not well formed.

    Grammar rule: --> LPAREN ( (COMMA )*)? RPAREN RBRACK

    ** PRECOND:**

    • the current GToken tag is LPAREN
    • the name tag is either UPPERID or LOWERID
    • the described element starting here and all its parts are well formed

    Parameters

    Returns ASTDefProcedure | ASTDefFunction

  • Private

    Parse a sequence of elements given by parseElement, separated by the separator and ending in the rightDelimiter (not parsed), if there is a well formed one starting at the current token. It fails if current token does not start the described element, or if it or any of its parts are not well formed.

    OBSERVATIONS:

    • The closing token, rightDelimiter, is NOT parsed to allow better structure at the caller

    Grammar rule: (rightDelimiter, separator, element) --> (element (separator element)*)? rightDelimiter

    ** PRECOND:**

    • the current GToken tag is either one of elementStarters or rightDelimiter
    • the described element starting here and all its parts are well formed

    Type Parameters

    • T

    Parameters

    • rightDelimiter: symbol
    • separator: symbol
    • parseElement: (() => T)
        • (): T
        • Returns T

    • constructionElement: GErrorElement

    Returns T[]

  • Private

    Parse the final part of a Range, if there is a well formed one starting at the current GToken. It fails if current GToken does not start the described element, or if it or any of its parts are not well formed.

    OBSERVATION: The closing token, RBRACK, is parsed to allow better structure at the caller.

    Grammar rule: --> RANGE RBRACK

    ** PRECOND:**

    • the current GToken tag is RANGE
    • the described element starting here and all its parts are well formed

    Parameters

    Returns ASTExprListRange

  • Private

    Parse a sequence of Expressions, starting and separated by commas and ending in a right bracket, if there is a well formed one starting at the current GToken. It fails if current GToken does not start the described element, or if it or any of its parts are not well formed.

    OBSERVATION: The closing token, RBRACK, is parsed to allow better structure at the caller.

    Grammar rule: --> (COMMA )* RBRACK

    ** PRECOND:**

    • the current GToken tag is COMMA or RBRACK
    • the described element starting here and all its parts are well formed

    Parameters

    Returns ASTExprLiteralList

  • Private

    Parse the name list (a parameter list of a procedure or function definition, or a tuple of variables on a let assignment), if there is a well formed one starting at the current GToken. It fails if current GToken does not start the described element, or if it or any of its parts are not well formed.

    Grammar rule: --> ( (COMMA )*)?

    ** PRECOND:**

    • the current GToken tag is LPAREN
    • the name tag is either UPPERID or LOWERID
    • the described element starting here and all its parts are well formed

    Parameters

    • construction: string
    • name: string = ''

    Returns Token[]

  • Private

    Parse a sequence of elements given by parseElement, starting and separated by the separator, if there is a well formed one starting at the current GToken. It fails if current GToken does not start the described element, or if it or any of its parts are not well formed.

    Grammar rule: (separator, element) --> (separator element)*

    ** PRECOND:**

    • the current GToken tag is separator
    • the described element starting here and all its parts are well formed

    Type Parameters

    • T

    Parameters

    • separator: symbol
    • prefix: T[]
    • parseElement: (() => T)
        • (): T
        • Returns T

    Returns T[]

  • Private

    Parse a switch branch if there is a well formed one starting at the current GToken. It fails if current GToken does not start the described element, or if it or any of its parts are not well formed.

    Grammar rule: --> ARROW

    ** PRECOND:**

    • the described element starting here and all its parts are well formed

    Parameters

    • construction: string
    • name: string = ''

    Returns ASTStmtBranch

  • Private

    Parse a sequence of switch branches if there is a well formed one starting at the current GToken. It fails if current GToken does not start the described element, or if it or any of its parts are not well formed.

    Grammar rule: --> ( ARROW )*

    ** PRECOND:**

    • the described element starting here and all its parts are well formed

    Parameters

    • construction: string
    • name: string = ''

    Returns ASTStmtBranch[]

Implementation: Parsing constructions (Definitions)

  • Private

    Parse a FUNTION definition, if there is a well formed one starting at the current GToken. It fails if current GToken does not start the described element, or if it or any of its parts are not well formed.

    Grammar rule: := FUNTION LPAREN ( (, )*)? RPAREN

    ** PRECOND:**

    • the current GToken tag is FUNCTION
    • the described element starting here and all its parts are well formed

    Returns ASTDefFunction

  • Private

    Parse a PROCEDURE definition, if there is a well formed one starting at the current GToken. It fails if current GToken does not start the described element, or if it or any of its parts are not well formed.

    Grammar rule: := PROCEDURE LPAREN ( (, )*)? RPAREN

    ** PRECOND:**

    • the current GToken tag is PROCEDURE
    • the described element starting here and all its parts are well formed

    Returns ASTDefProcedure

  • Private

    Parse a PROGRAM definition, if there is a well formed one starting at the current GToken. It fails if current GToken does not start the described element, or if it or any of its parts are not well formed.

    Grammar rule: := PROGRAM

    ** PRECOND:**

    • the current GToken tag is PROGRAM
    • the described element starting here and all its parts are well formed

    Returns ASTDefProgram

  • Private

    TO DO: Complete ** PRECOND:**

    • this._currentGToken has TYPE tag

    Returns ASTDefType

Implementation: Parsing constructions (Expressions)

  • Private

    Parse an Expression atom, if there is a well formed one starting at the current GToken. It fails if current GToken does not start the described element, or if it or any of its parts are not well formed.

    Grammar rule: := | | | | | | | | | LPAREN RPAREN | | |

    PRECONDITION:

    • the current GToken is one of those starting an Expression atom
    • the described element starting here and all its parts are well formed

    Returns ASTExpr

  • Private

    Parse an Ellipsis for Expressions, if there is one starting at the current GToken. It fails if current GToken does not start the described element.

    Grammar rule: := ELLIPSIS

    ** PRECOND:**

    • the current GToken tag is ELLIPSIS

    Returns ASTExprFunctionCall

  • Private

    Parse a Literal List or a List Range, if there is a well formed one starting at the current GToken. It fails if current GToken does not start the described element, or if it or any of its parts are not well formed.

    Grammar rules: := LBRACK ( (COMMA )*)? RBRACK := LBRACK (COMMA )? RANGE RBRACK

    OBSERVATION: The closing token, RBRACK, is parsed in the endings operations to allow better code structure.

    ** PRECOND:**

    • the current GToken tag is LBRACK
    • the described element starting here and all its parts are well formed

    Returns ASTExprLiteralList | ASTExprListRange

  • Private

    Parse a Literal Number expression, if there is a well formed one starting at the current GToken. It fails if current GToken does not start the described element, or if it or any of its parts are not well formed.

    Grammar rule: :=

    ** PRECOND:**

    • the current GToken tag is NUM
    • the described element starting here and all its parts are well formed

    Returns ASTExprLiteralNumber

  • Private

    Parse a Literal String expression, if there is a well formed one starting at the current GToken. It fails if current GToken does not start the described element, or if it or any of its parts are not well formed.

    Grammar rule: :=

    ** PRECOND:**

    • the current GToken tag is STRING
    • the described element starting here and all its parts are well formed

    Returns ASTExprLiteralString

  • Private

    Parse a Tuple or a single Expression surrounded by parentheses, if there is a well formed one starting at the current GToken. It fails if current GToken does not start the described element, or if it or any of its parts are not well formed.

    Grammar rules: --> LPAREN RPAREN := LPAREN ( (COMMA <Expr)+)? RPAREN

    ** PRECOND:**

    • the current GToken tag is LPAREN
    • the described element starting here and all its parts are well formed

    Parameters

    • mayBeEmpty: boolean = true

    Returns ASTExpr | ASTExprTuple

  • Private

    Parse a Function Call expression, if there is a well formed one starting at the current GToken. It fails if current GToken does not start the described element, or if it or any of its parts are not well formed.

    Grammar rule: := := LPAREN ( (COMMA )*)? RPAREN

    ** PRECOND:**

    • the current GToken tag is LOWERID
    • the described element starting here and all its parts are well formed

    Returns ASTExprFunctionCall | ASTExprVariable

Implementation: Parsing constructions (Parts)

  • Private

    TO DO: Complete Parse a pattern, if there is a well formed one starting at the current GToken. It fails if current GToken does not start the described element, or if it or any of its parts are not well formed.

    Grammar rule: := | | <<>>

    ** PRECOND:**

    • the current GToken is one of those starting a Pattern
    • the described element starting here and all its parts are well formed

    Returns ASTPattern

  • Private

    Parse a pattern variable, if there is a well formed one starting at the current GToken. It fails if current GToken does not start the described element.

    Grammar rule: :=

    ** PRECOND:**

    • the current GToken tag is LOWERID
    • the described element starting here and all its parts are well formed

    Returns ASTPatternVariable

  • Private

    Parse a pattern wildcard, if there is a well formed one starting at the current GToken. It fails if current GToken does not start the described element.

    Grammar rule: := UNDERSCORE

    ** PRECOND:**

    • the current GToken tag is UNDERSCORE
    • the described element starting here and all its parts are well formed

    Returns ASTPatternWildcard

  • Private

    Parse a conditional statement part of an if-then-else construct, if there is a well formed one starting at the current GToken. It fails if current GToken does not start the described element, or if it or any of its parts are not well formed.

    Grammar rule: := LPAREN RPAREN (THEN)?

    ** PRECOND:**

    • the current GToken tag is LPAREN
    • the described element starting here and all its parts are well formed

    Parameters

    • construction: string

    Returns ASTStmtConditional

Implementation: Parsing constructions (Statements)

  • Private

    Parse an Assign Variable statement, if there is a well formed one starting at the current GToken. It fails if current GToken does not start the described element, or if it or any of its parts are not well formed.

    Grammar rule: := ASSIGN

    ** PRECOND:**

    • the current GToken tag is LOWERID
    • the described element starting here and all its parts are well formed

    Returns ASTStmtAssignVariable

  • Private

    Parse a block of statements, if there is a well formed one starting at the current GToken. It fails if current GToken does not start the described element, or if it or any of its parts are not well formed.

    Grammar rule: := LBRACE * RBRACE

    ** PRECOND:**

    • the current GToken tag is LBRACE
    • the described element starting here and all its parts are well formed

    Returns ASTStmtBlock

  • Private

    Parse an Ellipsis for Statements, if there is one starting at the current GToken. It fails if current GToken does not start the described element.

    Grammar rule: := ELLIPSIS

    ** PRECOND:**

    • the current GToken tag is ELLIPSIS

    Returns ASTStmtProcedureCall

  • Private

    Parse a FOREACH statement, if there is a well formed one starting at the current GToken. It fails if current GToken does not start the described element, or if it or any of its parts are not well formed.

    Grammar rule: := FOREACH IN

    ** PRECOND:**

    • the current GToken tag is FOREACH
    • the described element starting here and all its parts are well formed

    Returns ASTStmtForeach

  • Private

    Parse a IF-THEN-ELSE statement, if there is a well formed one starting at the current GToken. It fails if current GToken does not start the described element, or if it or any of its parts are not well formed.

    Grammar rule: := IF (ELSEIF )+ (ELSE )?

    ** PRECOND:**

    • the current GToken tag is IF

    Returns ASTStmtIf

  • Private

    Parse a LET statement, if there is a well formed one starting at the current GToken. It fails if current GToken does not start the described element, or if it or any of its parts are not well formed.

    Grammar rule: := LET LPAREN (COMMA )* RPAREN ASSIGN

    ** PRECOND:**

    • the current GToken tag is LET
    • the described element starting here and all its parts are well formed

    Returns ASTStmtAssignTuple

  • Private

    Parse a Procedure Call statement, if there is a well formed one starting at the current GToken. It fails if current GToken does not start the described element, or if it or any of its parts are not well formed.

    Grammar rule: := LPAREN ( (COMMA )*)? RPAREN

    ** PRECOND:**

    • the current GToken tag is UPPERID
    • the described element starting here and all its parts are well formed

    Returns ASTStmtProcedureCall

  • Private

    Parse a REPEAT statement, if there is a well formed one starting at the current GToken. It fails if current GToken does not start the described element, or if it or any of its parts are not well formed.

    Grammar rule: := REPEAT LPAREN RPAREN

    ** PRECOND:**

    • the current GToken tag is REPEAT
    • the described element starting here and all its parts are well formed

    Returns ASTStmtRepeat

  • Private

    Parse a RETURN statement, if there is a well formed one starting at the current GToken. It fails if current GToken does not start the described element, or if it or any of its parts are not well formed.

    Grammar rule: := RETURN LPAREN (COMMA )* RPAREN

    ** PRECOND:**

    • the current GToken tag is RETURN
    • the described element starting here and all its parts are well formed

    Returns ASTStmtReturn

  • Private

    Parse a SWITCH statement, if there is a well formed one starting at the current GToken. It fails if current GToken does not start the described element, or if it or any of its parts are not well formed.

    Grammar rule: := SWITCH LPAREN RPAREN TO? LBRACE ( ARROW <Stmt-Block)* RBRACE

    ** PRECOND:**

    • the current GToken tag is SWITCH
    • the described element starting here and all its parts are well formed

    Returns ASTStmtSwitch

  • Private

    Parse a WHILE statement, if there is a well formed one starting at the current GToken. It fails if current GToken does not start the described element, or if it or any of its parts are not well formed.

    Grammar rule: := WHILE LPAREN RPAREN

    ** PRECOND:**

    • the current GToken tag is WHILE
    • the described element starting here and all its parts are well formed

    Returns ASTStmtWhile

Implementation: Properties -- State

_currentGToken: Token

TO DO: Complete

_langWords: Words

TO DO: Complete

_lexer: Lexer

TO DO: Complete