Skip to content

Commit

Permalink
Centralize keywords (hedyorg#463)
Browse files Browse the repository at this point in the history
This PR puts all symbols and all textual keywords in the first grammar file. That way we can potentially translate them all to Dutch or French or Swahili :)
  • Loading branch information
Felienne authored Jun 5, 2021
1 parent 40f290e commit c113129
Show file tree
Hide file tree
Showing 63 changed files with 1,952 additions and 1,031 deletions.
84 changes: 84 additions & 0 deletions grammars-Total/level10-Total.lark
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
%import common.CNAME -> NAME
%import common.DIGIT
%import common.LETTER
%import common.NEWLINE
%import common.SIGNED_INT -> NUMBER
%import common.WS_INLINE
?atom: NUMBER | var //TODO: means we cannot assign strings with spaces? would we want that?
?product: atom | product _SPACE* _MULTIPLY _SPACE* atom -> multiplication | product _SPACE* _DIVIDE _SPACE* atom -> division
?sum: product | sum _SPACE* _ADD _SPACE* product -> addition | sum _SPACE* _SUBTRACT _SPACE* product -> substraction
PUNCTUATION: _EXCLAMATION_MARK | _QUESTION_MARK | _PERIOD //uppercase places tokens in tree
_ADD: "+"
_AND: "and"
_ASK : "ask"
_AT: "at"
_COLON: ":"
_COMMA: ","
_DIVIDE: "/"
_DOUBLE_EQUALS: "=="
_ECHO : "echo"
_ELIF: "elif"
_ELSE: "else"
_EOL: "\r"?"\n"
_EQUALS: "="
_EXCLAMATION_MARK: "!"
_FOR: "for"
_HASH: "#"
_IF: "if"
_IN: "in"
_INPUT: "input"
_IS: "is"
_LARGER: ">"
_LARGER_EQUALS: ">="
_LEFT_BRACKET : "("
_LEFT_SQUARE_BRACKET : "["
_LENGTH: "length"
_MULTIPLY: "*"
_NOT_EQUALS: "!="
_OR: "or"
_PERIOD: "."
_PRINT : "print"
_QUESTION_MARK: "?"
_RANGE: "range"
_REPEAT: "repeat"
_RIGHT_BRACKET : ")"
_RIGHT_SQUARE_BRACKET : "]"
_SMALLER : "<"
_SMALLER_EQUALS : "<="
_SPACE: " "
_STEP: "step"
_SUBTRACT: "-"
_TIMES: "times"
_TO: "to"
_WHILE: "while"
ask: var _SPACE _IS _SPACE _ASK _SPACE textwithspaces*
assign: var _SPACE _IS _SPACE sum | var _SPACE _IS _SPACE textwithoutspaces
assign_list: var _SPACE _IS _SPACE textwithspaces (("," _SPACE |",") textwithspaces)+
command: print | ifs (elifs)* elses? | ask | for_loop | assign_list | list_access_var | assign
condition: (equality_check|in_list_check) (_SPACE _AND _SPACE condition)*
echo: _ECHO (_SPACE text)?
elifs: _EOL _SPACE* _ELIF _SPACE condition _COLON _EOL (_SPACE+ command) (_EOL _SPACE+ command)* _EOL "end-block"
elses: _EOL _SPACE* _ELSE _COLON _EOL (_SPACE+ command) (_EOL _SPACE+ command)* _EOL "end-block"
equality_check: textwithoutspaces _SPACE _IS _SPACE textwithoutspaces
for_loop: _FOR _SPACE (NAME | var) _SPACE _IN _SPACE _RANGE _SPACE (NUMBER | var) _SPACE _TO _SPACE (NUMBER | var) (_SPACE _STEP _SPACE (NUMBER | var))? _COLON _EOL (_SPACE+ command) (_EOL _SPACE+ command)* _EOL "end-block"
ifelse: _IF _SPACE condition _SPACE command _SPACE _ELSE _SPACE command
ifs: _IF _SPACE condition _COLON _EOL (_SPACE+ command) (_EOL _SPACE+ command)* _EOL "end-block"//'if' cannot be used in Python, hence the name of the rule is 'ifs'
in_list_check: textwithoutspaces _SPACE _IN _SPACE var
index : NUMBER //todo FH-June 21 : why does this need its own rule? can't it just be a token
invalid: textwithoutspaces _SPACE textwithspaces
invalid_space: _SPACE+ text
list_access: var " at " (index | random) -> list_access //todo: could be merged with list_access_var?
list_access_var: var _SPACE _IS _SPACE var _SPACE _AT _SPACE (index | random)
print: _PRINT _SPACE (quoted_text | list_access | var | sum) (_SPACE (quoted_text | list_access | var | sum))*
print_no_quotes: _PRINT _SPACE (textwithoutspaces | list_access | var) (_SPACE (textwithoutspaces | list_access | var))* -> print_nq
program: _EOL* command (_SPACE)* (_EOL+ command (_SPACE)*)* _EOL* //lines may end on spaces and might be separated by many newlines
punctuation : PUNCTUATION -> punctuation
quoted_text: /'((?:[^\\']|\\.)*)'/ -> text //text can be between single quotes, but quotes may be escaped with \
quoted_text_no_escape: /'([^']*)'/ -> text //simply all between quotes should this be used at earlier levels?
random : "random" //random needs to appear in the tree for further processing so does not start with _ or is uppercase
repeat: _REPEAT _SPACE (NUMBER | var) _SPACE _TIMES _EOL (_SPACE+ command) (_EOL _SPACE+ command)*
start: program
text: /([^\n,]+)/ -> text
textwithoutspaces: /([^\n, :*+-\/]+)/ -> text
textwithspaces: /([^\n,]+)/ -> text //anything can be parsed except for a newline and a comma for list separators
var: NAME -> var
85 changes: 85 additions & 0 deletions grammars-Total/level11-Total.lark
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
%import common.CNAME -> NAME
%import common.DIGIT
%import common.LETTER
%import common.NEWLINE
%import common.SIGNED_INT -> NUMBER
%import common.WS_INLINE
?atom: NUMBER | var //TODO: means we cannot assign strings with spaces? would we want that?
?product: atom | product _SPACE* _MULTIPLY _SPACE* atom -> multiplication | product _SPACE* _DIVIDE _SPACE* atom -> division
?sum: product | sum _SPACE* _ADD _SPACE* product -> addition | sum _SPACE* _SUBTRACT _SPACE* product -> substraction
PUNCTUATION: _EXCLAMATION_MARK | _QUESTION_MARK | _PERIOD //uppercase places tokens in tree
_ADD: "+"
_AND: "and"
_ASK : "ask"
_AT: "at"
_COLON: ":"
_COMMA: ","
_DIVIDE: "/"
_DOUBLE_EQUALS: "=="
_ECHO : "echo"
_ELIF: "elif"
_ELSE: "else"
_EOL: "\r"?"\n"
_EQUALS: "="
_EXCLAMATION_MARK: "!"
_FOR: "for"
_HASH: "#"
_IF: "if"
_IN: "in"
_INPUT: "input"
_IS: "is"
_LARGER: ">"
_LARGER_EQUALS: ">="
_LEFT_BRACKET : "("
_LEFT_SQUARE_BRACKET : "["
_LENGTH: "length"
_MULTIPLY: "*"
_NOT_EQUALS: "!="
_OR: "or"
_PERIOD: "."
_PRINT : "print"
_QUESTION_MARK: "?"
_RANGE: "range"
_REPEAT: "repeat"
_RIGHT_BRACKET : ")"
_RIGHT_SQUARE_BRACKET : "]"
_SMALLER : "<"
_SMALLER_EQUALS : "<="
_SPACE: " "
_STEP: "step"
_SUBTRACT: "-"
_TIMES: "times"
_TO: "to"
_WHILE: "while"
ask: var _SPACE _IS _SPACE _ASK _SPACE textwithspaces*
assign: var _SPACE _IS _SPACE sum | var _SPACE _IS _SPACE textwithoutspaces
assign_list: var _SPACE _IS _SPACE textwithspaces (("," _SPACE |",") textwithspaces)+
command: print | ifs (elifs)* elses? | input | for_loop | assign_list | list_access_var | assign
condition: (equality_check|in_list_check) (_SPACE _AND _SPACE condition)*
echo: _ECHO (_SPACE text)?
elifs: _EOL _SPACE* _ELIF _SPACE condition _COLON _EOL (_SPACE+ command) (_EOL _SPACE+ command)* _EOL "end-block"
elses: _EOL _SPACE* _ELSE _COLON _EOL (_SPACE+ command) (_EOL _SPACE+ command)* _EOL "end-block"
equality_check: textwithoutspaces _SPACE _IS _SPACE textwithoutspaces
for_loop: _FOR _SPACE (NAME | var) _SPACE _IN _SPACE _RANGE _LEFT_BRACKET (NUMBER | var) (_COMMA _SPACE|_COMMA) (NUMBER | var) ((_COMMA _SPACE |_COMMA) (NUMBER | var))? _RIGHT_BRACKET _COLON _EOL (_SPACE+ command) (_EOL _SPACE+ command)* _EOL "end-block"
ifelse: _IF _SPACE condition _SPACE command _SPACE _ELSE _SPACE command
ifs: _IF _SPACE condition _COLON _EOL (_SPACE+ command) (_EOL _SPACE+ command)* _EOL "end-block"//'if' cannot be used in Python, hence the name of the rule is 'ifs'
in_list_check: textwithoutspaces _SPACE _IN _SPACE var
index : NUMBER //todo FH-June 21 : why does this need its own rule? can't it just be a token
input: var _SPACE _IS _SPACE _INPUT _LEFT_BRACKET (quoted_text | list_access | var | sum) (_SPACE (quoted_text | list_access | var | sum))* _RIGHT_BRACKET
invalid: textwithoutspaces _SPACE textwithspaces
invalid_space: _SPACE+ text
list_access: var " at " (index | random) -> list_access //todo: could be merged with list_access_var?
list_access_var: var _SPACE _IS _SPACE var _SPACE _AT _SPACE (index | random)
print: _PRINT _LEFT_BRACKET (quoted_text | list_access | var | sum) (_SPACE (quoted_text | list_access | var | sum))* _RIGHT_BRACKET
print_no_quotes: _PRINT _SPACE (textwithoutspaces | list_access | var) (_SPACE (textwithoutspaces | list_access | var))* -> print_nq
program: _EOL* command (_SPACE)* (_EOL+ command (_SPACE)*)* _EOL* //lines may end on spaces and might be separated by many newlines
punctuation : PUNCTUATION -> punctuation
quoted_text: /'((?:[^\\']|\\.)*)'/ -> text //text can be between single quotes, but quotes may be escaped with \
quoted_text_no_escape: /'([^']*)'/ -> text //simply all between quotes should this be used at earlier levels?
random : "random" //random needs to appear in the tree for further processing so does not start with _ or is uppercase
repeat: _REPEAT _SPACE (NUMBER | var) _SPACE _TIMES _EOL (_SPACE+ command) (_EOL _SPACE+ command)*
start: program
text: /([^\n,]+)/ -> text
textwithoutspaces: /([^\n, :*+-\/]+)/ -> text
textwithspaces: /([^\n,]+)/ -> text //anything can be parsed except for a newline and a comma for list separators
var: NAME -> var
86 changes: 86 additions & 0 deletions grammars-Total/level12-Total.lark
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
%import common.CNAME -> NAME
%import common.DIGIT
%import common.LETTER
%import common.NEWLINE
%import common.SIGNED_INT -> NUMBER
%import common.WS_INLINE
?atom: NUMBER | var //TODO: means we cannot assign strings with spaces? would we want that?
?product: atom | product _SPACE* _MULTIPLY _SPACE* atom -> multiplication | product _SPACE* _DIVIDE _SPACE* atom -> division
?sum: product | sum _SPACE* _ADD _SPACE* product -> addition | sum _SPACE* _SUBTRACT _SPACE* product -> substraction
PUNCTUATION: _EXCLAMATION_MARK | _QUESTION_MARK | _PERIOD //uppercase places tokens in tree
_ADD: "+"
_AND: "and"
_ASK : "ask"
_AT: "at"
_COLON: ":"
_COMMA: ","
_DIVIDE: "/"
_DOUBLE_EQUALS: "=="
_ECHO : "echo"
_ELIF: "elif"
_ELSE: "else"
_EOL: "\r"?"\n"
_EQUALS: "="
_EXCLAMATION_MARK: "!"
_FOR: "for"
_HASH: "#"
_IF: "if"
_IN: "in"
_INPUT: "input"
_IS: "is"
_LARGER: ">"
_LARGER_EQUALS: ">="
_LEFT_BRACKET : "("
_LEFT_SQUARE_BRACKET : "["
_LENGTH: "length"
_MULTIPLY: "*"
_NOT_EQUALS: "!="
_OR: "or"
_PERIOD: "."
_PRINT : "print"
_QUESTION_MARK: "?"
_RANGE: "range"
_REPEAT: "repeat"
_RIGHT_BRACKET : ")"
_RIGHT_SQUARE_BRACKET : "]"
_SMALLER : "<"
_SMALLER_EQUALS : "<="
_SPACE: " "
_STEP: "step"
_SUBTRACT: "-"
_TIMES: "times"
_TO: "to"
_WHILE: "while"
ask: var _SPACE _IS _SPACE _ASK _SPACE textwithspaces*
assign: var _SPACE _IS _SPACE sum | var _SPACE _IS _SPACE textwithoutspaces | list_access _SPACE _IS _SPACE sum | list_access _SPACE _IS _SPACE textwithoutspaces
assign_list: var _SPACE _IS _SPACE _LEFT_SQUARE_BRACKET quoted_text ((", "|",") quoted_text)+ _RIGHT_SQUARE_BRACKET
change_list_item: var _LEFT_SQUARE_BRACKET (index | var) _RIGHT_SQUARE_BRACKET _SPACE _IS _SPACE (var | textwithoutspaces)
command: print | ifs (elifs)* elses? | input | for_loop | assign_list | list_access_var | change_list_item | assign
condition: (equality_check|in_list_check) (_SPACE _AND _SPACE condition)*
echo: _ECHO (_SPACE text)?
elifs: _EOL _SPACE* _ELIF _SPACE condition _COLON _EOL (_SPACE+ command) (_EOL _SPACE+ command)* _EOL "end-block"
elses: _EOL _SPACE* _ELSE _COLON _EOL (_SPACE+ command) (_EOL _SPACE+ command)* _EOL "end-block"
equality_check: (textwithoutspaces | list_access) _SPACE _IS _SPACE (textwithoutspaces | list_access)
for_loop: _FOR _SPACE (NAME | var) _SPACE _IN _SPACE _RANGE _LEFT_BRACKET (NUMBER | var) (_COMMA _SPACE|_COMMA) (NUMBER | var) ((_COMMA _SPACE |_COMMA) (NUMBER | var))? _RIGHT_BRACKET _COLON _EOL (_SPACE+ command) (_EOL _SPACE+ command)* _EOL "end-block"
ifelse: _IF _SPACE condition _SPACE command _SPACE _ELSE _SPACE command
ifs: _IF _SPACE condition _COLON _EOL (_SPACE+ command) (_EOL _SPACE+ command)* _EOL "end-block"//'if' cannot be used in Python, hence the name of the rule is 'ifs'
in_list_check: textwithoutspaces _SPACE _IN _SPACE var
index : NUMBER //todo FH-June 21 : why does this need its own rule? can't it just be a token
input: var _SPACE _IS _SPACE _INPUT _LEFT_BRACKET (quoted_text | list_access | var | sum) (_SPACE (quoted_text | list_access | var | sum))* _RIGHT_BRACKET
invalid: textwithoutspaces _SPACE textwithspaces
invalid_space: _SPACE+ text
list_access: var _LEFT_SQUARE_BRACKET (index | random | var) _RIGHT_SQUARE_BRACKET
list_access_var: var _SPACE _IS _SPACE var _LEFT_SQUARE_BRACKET (index | random | var) _RIGHT_SQUARE_BRACKET
print: _PRINT _LEFT_BRACKET (quoted_text | list_access | var | sum) (_SPACE (quoted_text | list_access | var | sum))* _RIGHT_BRACKET
print_no_quotes: _PRINT _SPACE (textwithoutspaces | list_access | var) (_SPACE (textwithoutspaces | list_access | var))* -> print_nq
program: _EOL* command (_SPACE)* (_EOL+ command (_SPACE)*)* _EOL* //lines may end on spaces and might be separated by many newlines
punctuation : PUNCTUATION -> punctuation
quoted_text: /'((?:[^\\']|\\.)*)'/ -> text //text can be between single quotes, but quotes may be escaped with \
quoted_text_no_escape: /'([^']*)'/ -> text //simply all between quotes should this be used at earlier levels?
random : "random" //random needs to appear in the tree for further processing so does not start with _ or is uppercase
repeat: _REPEAT _SPACE (NUMBER | var) _SPACE _TIMES _EOL (_SPACE+ command) (_EOL _SPACE+ command)*
start: program
text: /([^\n,]+)/ -> text
textwithoutspaces: /([^\n, :*+-\/]+)/ -> text
textwithspaces: /([^\n,]+)/ -> text //anything can be parsed except for a newline and a comma for list separators
var: NAME -> var
86 changes: 86 additions & 0 deletions grammars-Total/level13-Total.lark
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
%import common.CNAME -> NAME
%import common.DIGIT
%import common.LETTER
%import common.NEWLINE
%import common.SIGNED_INT -> NUMBER
%import common.WS_INLINE
?atom: NUMBER | var //TODO: means we cannot assign strings with spaces? would we want that?
?product: atom | product _SPACE* _MULTIPLY _SPACE* atom -> multiplication | product _SPACE* _DIVIDE _SPACE* atom -> division
?sum: product | sum _SPACE* _ADD _SPACE* product -> addition | sum _SPACE* _SUBTRACT _SPACE* product -> substraction
PUNCTUATION: _EXCLAMATION_MARK | _QUESTION_MARK | _PERIOD //uppercase places tokens in tree
_ADD: "+"
_AND: "and"
_ASK : "ask"
_AT: "at"
_COLON: ":"
_COMMA: ","
_DIVIDE: "/"
_DOUBLE_EQUALS: "=="
_ECHO : "echo"
_ELIF: "elif"
_ELSE: "else"
_EOL: "\r"?"\n"
_EQUALS: "="
_EXCLAMATION_MARK: "!"
_FOR: "for"
_HASH: "#"
_IF: "if"
_IN: "in"
_INPUT: "input"
_IS: "is"
_LARGER: ">"
_LARGER_EQUALS: ">="
_LEFT_BRACKET : "("
_LEFT_SQUARE_BRACKET : "["
_LENGTH: "length"
_MULTIPLY: "*"
_NOT_EQUALS: "!="
_OR: "or"
_PERIOD: "."
_PRINT : "print"
_QUESTION_MARK: "?"
_RANGE: "range"
_REPEAT: "repeat"
_RIGHT_BRACKET : ")"
_RIGHT_SQUARE_BRACKET : "]"
_SMALLER : "<"
_SMALLER_EQUALS : "<="
_SPACE: " "
_STEP: "step"
_SUBTRACT: "-"
_TIMES: "times"
_TO: "to"
_WHILE: "while"
ask: var _SPACE _IS _SPACE _ASK _SPACE textwithspaces*
assign: var _SPACE _IS _SPACE sum | var _SPACE _IS _SPACE textwithoutspaces | list_access _SPACE _IS _SPACE sum | list_access _SPACE _IS _SPACE textwithoutspaces
assign_list: var _SPACE _IS _SPACE _LEFT_SQUARE_BRACKET quoted_text ((", "|",") quoted_text)+ _RIGHT_SQUARE_BRACKET
change_list_item: var _LEFT_SQUARE_BRACKET (index | var) _RIGHT_SQUARE_BRACKET _SPACE _IS _SPACE (var | textwithoutspaces)
command: print | ifs (elifs)* elses? | input | for_loop | assign_list | list_access_var | change_list_item | assign
condition: (equality_check|in_list_check) (_SPACE _AND _SPACE condition)*
echo: _ECHO (_SPACE text)?
elifs: _EOL _SPACE* _ELIF _SPACE condition _COLON _EOL (_SPACE+ command) (_EOL _SPACE+ command)* _EOL "end-block"
elses: _EOL _SPACE* _ELSE _COLON _EOL (_SPACE+ command) (_EOL _SPACE+ command)* _EOL "end-block"
equality_check: (textwithoutspaces | list_access) _SPACE _IS _SPACE (textwithoutspaces | list_access)
for_loop: _FOR _SPACE (NAME | var) _SPACE _IN _SPACE _RANGE _LEFT_BRACKET (NUMBER | var) (_COMMA _SPACE|_COMMA) (NUMBER | var) ((_COMMA _SPACE |_COMMA) (NUMBER | var))? _RIGHT_BRACKET _COLON _EOL (_SPACE+ command) (_EOL _SPACE+ command)* _EOL "end-block"
ifelse: _IF _SPACE condition _SPACE command _SPACE _ELSE _SPACE command
ifs: _IF _SPACE condition _COLON _EOL (_SPACE+ command) (_EOL _SPACE+ command)* _EOL "end-block"//'if' cannot be used in Python, hence the name of the rule is 'ifs'
in_list_check: textwithoutspaces _SPACE _IN _SPACE var
index : NUMBER //todo FH-June 21 : why does this need its own rule? can't it just be a token
input: var _SPACE _IS _SPACE _INPUT _LEFT_BRACKET (quoted_text | list_access | var | sum) (_SPACE (quoted_text | list_access | var | sum))* _RIGHT_BRACKET
invalid: textwithoutspaces _SPACE textwithspaces
invalid_space: _SPACE+ text
list_access: var _LEFT_SQUARE_BRACKET (index | random | var) _RIGHT_SQUARE_BRACKET
list_access_var: var _SPACE _IS _SPACE var _LEFT_SQUARE_BRACKET (index | random | var) _RIGHT_SQUARE_BRACKET
print: _PRINT _LEFT_BRACKET (quoted_text | list_access | var | sum) (_SPACE (quoted_text | list_access | var | sum))* _RIGHT_BRACKET
print_no_quotes: _PRINT _SPACE (textwithoutspaces | list_access | var) (_SPACE (textwithoutspaces | list_access | var))* -> print_nq
program: _EOL* command (_SPACE)* (_EOL+ command (_SPACE)*)* _EOL* //lines may end on spaces and might be separated by many newlines
punctuation : PUNCTUATION -> punctuation
quoted_text: /'((?:[^\\']|\\.)*)'/ -> text //text can be between single quotes, but quotes may be escaped with \
quoted_text_no_escape: /'([^']*)'/ -> text //simply all between quotes should this be used at earlier levels?
random : "random" //random needs to appear in the tree for further processing so does not start with _ or is uppercase
repeat: _REPEAT _SPACE (NUMBER | var) _SPACE _TIMES _EOL (_SPACE+ command) (_EOL _SPACE+ command)*
start: program
text: /([^\n,]+)/ -> text
textwithoutspaces: /([^\n, :*+-\/]+)/ -> text
textwithspaces: /([^\n,]+)/ -> text //anything can be parsed except for a newline and a comma for list separators
var: NAME -> var
Loading

0 comments on commit c113129

Please sign in to comment.