Repository navigation
Expand file tree
/
Copy pathob.peg
More file actions
127 lines (122 loc) · 4.73 KB
/
Copy pathob.peg
File metadata and controls
127 lines (122 loc) · 4.73 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
Module <- Imports Decs Sequence? EOF
Imports <- (IMPORT QualId (COMMA QualId)*)*
QualId <- Id (DOT Id)*
Decs <- (EXPORT? Decl)*
Decl <- FunDec / VarDec / TypeDec
FunDec <- FUNCTION Id FunHdr EQ Expr
/ EXTERNAL FUNCTION Id FunHdr
FunHdr <- LPAREN (Param (COMMA Param)*)? RPAREN Annotation?
Param <- VAR? Id Annotation
Annotation <- COLON TypeId
TypeId <- QualId / ARRAY OF TypeId
VarDec <- VAR Id Annotation? EQ Expr
/ EXTERNAL VAR Id Annotation
TypeDec <- TYPE Id EQ TypeSpec
TypeSpec <- QualId / RecordSpec / UnionSpec / ArraySpec / FunSpec
RecordSpec <- LBRACE (FieldSpec (COMMA FieldSpec)*)? RBRACE
FieldSpec <- Id COLON QualId
UnionSpec <- VariantSpec (PIPE VariantSpec)+
VariantSpec <- Id RecordSpec?
ArraySpec <- ARRAY OF TypeId
FunSpec <- LPAREN (TypeId (COMMA TypeId)*)? RPAREN ARROW TypeId
Expr <- Assignment / IfExpr / CaseExpr / LetExpr / ForLoop
/ WhileLoop / Sequence / Disjunction
Assignment <- LValue BECOMES Expr
LValue <- Id (Subscript / Field)*
IfExpr <- IF Expr THEN Expr ELSE Expr
CaseExpr <- Case Expr OF CaseClause (PIPE CaseClause)* (ELSE Expr)? END
CaseClause <- CaseList ARROW Expr
CaseList <- CaseRange (COMMA CaseRange)*
CaseRange <- Case (ELIPSIS Case)?
Case <- QualId / Integer / Char
LetExpr <- LET Decl* IN Expr
ForLoop <- FOR Id BECOMES Expr TO Expr DO Expr
/ FOR Id IN Expr DO Expr
WhileLoop <- WHILE Expr DO Expr
Sequence <- BEGIN Expr (SEMICOLON? Expr)* END
Unit <- LPAREN RPAREN
Disjunction <- Conjunction (OR Conjunction)*
Conjunction <- Compare (AND Compare)*
Compare <- Sum ((EQ / LE / NE / LT / GE / GT) Sum)?
Sum <- Product ((PLUS / MINUS) Product)?
Product <- Negation ((SLASH / STAR / MOD) Negation)*
Negation <- MINUS? Factor
Factor <- Id Selector* / Number / String / Char / Abstraction (Args Selector*)?
/ LPAREN Expr RPAREN Selector* / Record Selector* / Array Selector*
Selector <- Subscript / Field / Args
Subscript <- LBRACKET Expr (COMMA Expr)* RBRACKET
Field <- DOT Id
Args <- LPAREN (Expr (COMMA Expr)*)? RPAREN
Abstraction <- LAMBDA FunHdr EQ Expr
Record <- QualId LBRACE (FieldDef (COMMA FieldDef)*)? RBRACE
FieldDef <- Id EQ Expr
Array <- LBRACKET (Expr (COMMA Expr)*)? RBRACKET
/ ARRAY LBRACKET Expr (COMMA Expr)* RBRACKET OF Expr
Keyword <- AND / ARRAY / BEGIN / DO / ELSE / END / EXPORT / EXTERNAL
/ FOR / FUNCTION / IF / IN / IMPORT / LAMBDA / LET
/ MOD / OF / OR / THEN / TO / TYPE / VAR / WHILE
AND <- 'and' !AlphaNum Spaces
ARRAY <- 'array' !AlphaNum Spaces
BEGIN <- 'begin' !AlphaNum Spaces
DO <- 'do' !AlphaNum Spaces
ELSE <- 'else' !AlphaNum Spaces
END <- 'end' !AlphaNum Spaces
EXPORT <- 'export' !AlphaNum Spaces
EXTERNAL <- 'external' !AlphaNum Spaces
FOR <- 'for' !AlphaNum Spaces
FUNCTION <- 'function' !AlphaNum Spaces
IF <- 'if' !AlphaNum Spaces
IN <- 'in' !AlphaNum Spaces
IMPORT <- 'import' !AlphaNum Spaces
LAMBDA <- 'lambda' !AlphaNum Spaces
LET <- 'let' !AlphaNum Spaces
MOD <- 'mod' !AlphaNum Spaces
OF <- 'of' !AlphaNum Spaces
OR <- 'or' !AlphaNum Spaces
THEN <- 'then' !AlphaNum Spaces
TO <- 'to' !AlphaNum Spaces
TYPE <- 'type' !AlphaNum Spaces
VAR <- 'var' !AlphaNum Spaces
WHILE <- 'while' !AlphaNum Spaces
ARROW <- '->' Spaces
MINUS <- '-' Spaces
PLUS <- '+' Spaces
SLASH <- '/' Spaces
STAR <- '*' Spaces
COMMA <- ',' Spaces
ELIPSIS <- '..' Spaces
DOT <- '.' Spaces
BECOMES <- ':=' Spaces
COLON <- ':' Spaces
EQ <- '=' Spaces
LE <- '<=' Spaces
NE <- '<>' Spaces
LT <- '<' Spaces
GE <- '>=' Spaces
GT <- '>' Spaces
PIPE <- '|' Spaces
LPAREN <- '(' Spaces
RPAREN <- ')' Spaces
LBRACE <- '{' Spaces
RBRACE <- '}' Spaces
LBRACKET <- '[' Spaces
RBRACKET <- ']' Spaces
SEMICOLON <- ';' Spaces
String <- '"' StringChar* '"' Spaces
Char <- "'" CharChar "'" Spaces
Number <- Real / Integer
Integer <- '0' [Xx] HexDigit+ Spaces
/ Digit+ Spaces
Real <- Digit+ '.' Digit+ ([Ee] ('-' / '+')? Digit+)? Spaces
Id <- !Keyword Alpha AlphaNum* Spaces
StringChar <- !["\\\n] . / Escape
CharChar <- !['\\\n] . / Escape
Escape <- '\\' ([abtnvfr"'\\] / x HexDigit HexDigit)
AlphaNum <- Alpha / Digit
Alpha <- [A-Za-z_]
Digit <- [0-9]
HexDigit <- Digit / [A-Fa-f]
Spaces <- (Space / Comment)*
Space <- [ \t\n\v\f\r]
Comment <- '(*' (Comment / (!'*)' .))* '*)'
EOF <- !.