369 lines
10 KiB
Plaintext
369 lines
10 KiB
Plaintext
unit wpParser;
|
|
|
|
parser TwpParser;
|
|
options
|
|
{
|
|
importVocab = wpLexer;
|
|
k = 2;
|
|
}
|
|
|
|
// ============================================================================
|
|
// prog
|
|
// ============================================================================
|
|
prog
|
|
: "program" id (LPAREN id (COLON id)* RPAREN)? SEMI block DOT
|
|
;
|
|
|
|
// ============================================================================
|
|
// block
|
|
// ============================================================================
|
|
block
|
|
: declarations compoundStmt
|
|
;
|
|
|
|
// ============================================================================
|
|
// declarations
|
|
// ============================================================================
|
|
declarations
|
|
:
|
|
( "label" UINT (COMMA UINT)* SEMI )?
|
|
( "const" (id EQ constant SEMI)+ )?
|
|
( "type" (id EQ typeSpec SEMI)+ )?
|
|
( "var" (id (COMMA id)* COLON typeSpec SEMI)+ )?
|
|
|
|
(
|
|
"procedure" id parameterList SEMI block SEMI
|
|
| "function" id parameterList COLON id SEMI block SEMI
|
|
)*
|
|
;
|
|
|
|
// ============================================================================
|
|
// statement
|
|
// ============================================================================
|
|
statement
|
|
: (UINT COLON)?
|
|
(
|
|
(variable ASSIGN) => assignmentStmt
|
|
| procedureCall
|
|
| compoundStmt
|
|
| ifStmt
|
|
| caseStmt
|
|
| whileStmt
|
|
| repeatStmt
|
|
| forStmt
|
|
| withStmt
|
|
| gotoStmt
|
|
)?
|
|
;
|
|
|
|
// ============================================================================
|
|
// assignmentStmt
|
|
// ============================================================================
|
|
assignmentStmt
|
|
: variable ASSIGN expression
|
|
;
|
|
|
|
// ============================================================================
|
|
// procedureCall
|
|
// ============================================================================
|
|
procedureCall
|
|
: id (LPAREN expression (widthSpec)? (COMMA expression (widthSpec)? )* RPAREN)?
|
|
;
|
|
|
|
// ============================================================================
|
|
// widthSpec
|
|
// ============================================================================
|
|
widthSpec
|
|
: (COLON UINT) (COLON UINT)?
|
|
;
|
|
|
|
|
|
// ============================================================================
|
|
// compoundStmt
|
|
// ============================================================================
|
|
compoundStmt
|
|
: "begin" (statement (SEMI statement)*)? "end"
|
|
;
|
|
|
|
// ============================================================================
|
|
// ifStmt
|
|
// ============================================================================
|
|
ifStmt
|
|
: "if" expression "then" statement
|
|
(
|
|
("else") => "else" statement
|
|
|
|
|
)
|
|
;
|
|
|
|
// ============================================================================
|
|
// caseStmt
|
|
// ============================================================================
|
|
caseStmt
|
|
: "case" expression "of"
|
|
( caseStmtItem (SEMI caseStmtItem)* )?
|
|
"end"
|
|
;
|
|
|
|
// ============================================================================
|
|
// caseStmtItem
|
|
// ============================================================================
|
|
caseStmtItem
|
|
: constant (COMMA constant)* COLON statement
|
|
;
|
|
|
|
// ============================================================================
|
|
// whileStmt
|
|
// ============================================================================
|
|
whileStmt
|
|
: "while" expression "do" statement
|
|
;
|
|
|
|
// ============================================================================
|
|
// repeatStmt
|
|
// ============================================================================
|
|
repeatStmt
|
|
: "repeat" (statement (SEMI statement)*)? "until" expression
|
|
;
|
|
|
|
// ============================================================================
|
|
// forStmt
|
|
// ============================================================================
|
|
forStmt
|
|
: "for" id ASSIGN expression ("to" | "downto") expression "do" statement
|
|
;
|
|
|
|
// ============================================================================
|
|
// withStmt
|
|
// ============================================================================
|
|
withStmt
|
|
: "with" variable (COMMA variable)* "do" statement
|
|
;
|
|
|
|
// ============================================================================
|
|
// gotoStmt
|
|
// ============================================================================
|
|
gotoStmt
|
|
: "goto" UINT
|
|
;
|
|
|
|
|
|
// ============================================================================
|
|
// parameterList
|
|
// ============================================================================
|
|
parameterList
|
|
: (
|
|
LPAREN
|
|
parameter (SEMI parameter)*
|
|
RPAREN
|
|
)?
|
|
;
|
|
|
|
// ============================================================================
|
|
// parameter
|
|
// ============================================================================
|
|
parameter
|
|
: ("var" | "function")? id (COMMA id)* COLON typeId
|
|
| "procedure" id (COMMA id)*
|
|
;
|
|
|
|
|
|
// ============================================================================
|
|
// expression
|
|
// ============================================================================
|
|
expression
|
|
: simpleExpression (relOp simpleExpression)*
|
|
;
|
|
|
|
// ============================================================================
|
|
// simpleExpression
|
|
// ============================================================================
|
|
simpleExpression
|
|
: (PLUS|MINUS)? term (addOp term)*
|
|
;
|
|
|
|
// ============================================================================
|
|
// term
|
|
// ============================================================================
|
|
term
|
|
: factor (mulOp factor)*
|
|
;
|
|
|
|
// ============================================================================
|
|
// factor
|
|
// ============================================================================
|
|
factor
|
|
: uNumber
|
|
| "nil"
|
|
| CHAR
|
|
| STRING
|
|
| (id LPAREN) => procedureCall
|
|
| variable
|
|
| LPAREN expression RPAREN
|
|
| "not" factor
|
|
| LBRACKET (expression (RANGE expression)? (COMMA expression (RANGE expression)? )* )? RBRACKET
|
|
;
|
|
|
|
// ============================================================================
|
|
// variable
|
|
// ============================================================================
|
|
variable
|
|
: variableId
|
|
(
|
|
LBRACKET expression (COMMA expression)* RBRACKET
|
|
| DOT fieldId
|
|
| PTR
|
|
)*
|
|
;
|
|
|
|
// ============================================================================
|
|
// fieldList
|
|
// ============================================================================
|
|
fieldList
|
|
: simpleFieldList (simpleFieldList)* (variantFieldList)?
|
|
|
|
|
;
|
|
|
|
// ============================================================================
|
|
// simpleFieldList
|
|
// ============================================================================
|
|
simpleFieldList
|
|
: id (COMMA id)* COLON typeSpec
|
|
;
|
|
|
|
// ============================================================================
|
|
// caseFieldList
|
|
// ============================================================================
|
|
variantFieldList
|
|
: "case" (id COLON)? typeId "of"
|
|
constant (COMMA constant)* COLON LPAREN fieldList RPAREN
|
|
(SEMI constant (COMMA constant)* COLON LPAREN fieldList RPAREN)*
|
|
;
|
|
|
|
// ============================================================================
|
|
// typeSpecification
|
|
// ============================================================================
|
|
typeSpec
|
|
: simpleType
|
|
| PTR typeId
|
|
| ("packed")?
|
|
(
|
|
"array" LBRACKET simpleType (COMMA simpleType)* RBRACKET "of" typeSpec
|
|
| "file" "of" typeSpec
|
|
| "set" "of" simpleType
|
|
| "record" fieldList "end"
|
|
)
|
|
;
|
|
|
|
// ============================================================================
|
|
// simpleType
|
|
// ============================================================================
|
|
simpleType
|
|
: (constant RANGE) => constant RANGE constant
|
|
| typeId
|
|
| LPAREN id (COMMA id)* RPAREN
|
|
;
|
|
|
|
// ============================================================================
|
|
// constant
|
|
// ============================================================================
|
|
constant
|
|
: (PLUS | MINUS)? (constantId | uNumber)
|
|
| CHAR
|
|
;
|
|
|
|
// ============================================================================
|
|
// unsignedConstant
|
|
// ============================================================================
|
|
uConstant
|
|
: constantId
|
|
| uNumber
|
|
| "nil"
|
|
| CHAR
|
|
;
|
|
|
|
// ============================================================================
|
|
// unumber
|
|
// ============================================================================
|
|
uNumber
|
|
: UINT
|
|
| UREAL
|
|
;
|
|
|
|
// ============================================================================
|
|
// uint
|
|
// ============================================================================
|
|
uInt
|
|
: UINT;
|
|
|
|
// ============================================================================
|
|
// fieldId
|
|
// ============================================================================
|
|
fieldId
|
|
: id
|
|
;
|
|
|
|
// ============================================================================
|
|
// variableId
|
|
// ============================================================================
|
|
variableId
|
|
: id
|
|
;
|
|
|
|
// ============================================================================
|
|
// typeId
|
|
// ============================================================================
|
|
typeId
|
|
: id
|
|
;
|
|
|
|
// ============================================================================
|
|
// constantId
|
|
// ============================================================================
|
|
constantId
|
|
: id
|
|
;
|
|
|
|
// ============================================================================
|
|
// id
|
|
// ============================================================================
|
|
id
|
|
: ID
|
|
;
|
|
|
|
// ============================================================================
|
|
// relOp
|
|
// ============================================================================
|
|
relOp
|
|
: GT
|
|
| LT
|
|
| GE
|
|
| LE
|
|
| NE
|
|
| EQ
|
|
| "in"
|
|
;
|
|
|
|
// ============================================================================
|
|
// addOp
|
|
// ============================================================================
|
|
addOp
|
|
: PLUS
|
|
| MINUS
|
|
| "or"
|
|
| "xor"
|
|
;
|
|
|
|
// ============================================================================
|
|
// mulOp
|
|
// ============================================================================
|
|
mulOp
|
|
: STAR
|
|
| SLASH
|
|
| "div"
|
|
| "mod"
|
|
| "and"
|
|
;
|
|
|
|
|