forked from hedyorg/hedy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
level1.lark
56 lines (46 loc) · 1.73 KB
/
level1.lark
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
//symbols. they start with an underscore so they don't appear in the parse tree (Lark convention)
_SPACE: " "+
_COMMA: _SPACE? (","|"،") _SPACE? //support latin and arabic comma;s and always allow these to be surrounded by spaces
_COLON: ":"
_LEFT_BRACKET : "("
_RIGHT_BRACKET : ")"
_LEFT_SQUARE_BRACKET : "["
_RIGHT_SQUARE_BRACKET : "]"
_HASH: "#"
_SMALLER : "<"
_LARGER: ">"
_EQUALS: _SPACE? "=" _SPACE? //always allow = to be surrounded by spaces
_DOUBLE_EQUALS: "=="
_NOT_EQUALS: "!="
_SMALLER_EQUALS : "<="
_LARGER_EQUALS: ">="
_EXCLAMATION_MARK: "!"
_QUESTION_MARK: "?"
_PERIOD: "."
_SINGLE_QUOTE: "'"
_MULTIPLY: "*"
_ADD: "+"
_SUBTRACT: "-"
_DIVIDE: "/"
start: program
program: _EOL* (command _EOL+)* command?
command: print | ask | echo | turtle | error_invalid_space | comment | error_invalid
print: _PRINT (_SPACE text)?
ask: _ASK (_SPACE text)?
echo: _ECHO (_SPACE text)?
turtle: _FORWARD (_SPACE (text | NUMBER))? -> forward | _TURN (_SPACE text)? -> turn
error_invalid_space: _SPACE text?
error_invalid: textwithoutspaces text?
comment: _HASH /([^\n]+)/
_EOL: "\r"?"\n"
text: /([^\n]+)/ -> text //anything can be parsed except for a newline
textwithoutspaces: /([^\n *+-\/]+)/ -> text //anything can be parsed except for spaces (plus: a newline)
%import common.SIGNED_INT -> INT
%import common.NUMBER -> NUMBER
// FH Sept 2021: More info on this variable format: https://www.unicode.org/reports/tr31/tr31-1.html
// Exact grammar stolen from: https://lark-parser.readthedocs.io/en/latest/classes.html
NAME: ID_START ID_CONTINUE*
ID_START: /[\p{Lu}\p{Ll}\p{Lt}\p{Lm}\p{Lo}\p{Nl}_]+/
ID_CONTINUE: ID_START | /[\p{Mn}\p{Mc}\p{Nd}\p{Pc}·]+/
// Internal symbol added by the preprocess_blocks function to indicate the end of blocks
_END_BLOCK: "end-block"