aboutsummaryrefslogtreecommitdiff
path: root/nomsu.peg
blob: 5ba75cd8feeb942bd3b7f9770f72717058968976 (plain)
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
file (File):
    {| shebang?
        (ignored_line %nl)*
        statement (nodent statement)*
        (%nl ignored_line)*
    (!. / (("" -> "Parse error") => error)?) |}

shebang: "#!" [^%nl]* %nl

statement: functioncall / expression

indented_block (Block):
    {| indent
            statement (nodent statement)*
        (dedent / (("" -> "Error while parsing block") => error))
    |}

inline_nomsu (Nomsu): "\" inline_expression
indented_nomsu (Nomsu): "\" expression

inline_expression:
    number / variable / inline_text / inline_list / inline_dict / inline_nomsu
    / ("(" %ws* (inline_functioncall / inline_expression) %ws* ")")
indented_expression:
    indented_text / indented_nomsu / indented_list / indented_dict / indented_block
expression:
    inline_expression / (":" %ws* (inline_functioncall / inline_expression)) / indented_expression

-- Function calls need at least one word in them
inline_functioncall (FunctionCall):
    {| (inline_expression %ws*)* word (%ws* (inline_expression / word))*
        (%ws* ":" %ws* (inline_functioncall / inline_expression))?|}
functioncall (FunctionCall):
    {| (expression (dotdot / %ws*))* word ((dotdot / %ws*) (expression / word))* |}

word (Word): { %operator / (!number plain_word) }

inline_text (Text):
    !('".."')
    '"' {|
        ({~ (('\"' -> '"') / ('\\' -> '\') / ('\..' -> '..') / %escaped_char / [^%nl\"])+ ~}
        / text_interpolation)*
    |} '"'
indented_text (Text):
    '".."' %ws* line_comment? %nl %gt_nodent? {|
        ({~
            (("\\" -> "\") / (("\" eol %nl %gt_nodent "..") -> "")
            / (%nl+ {~ %gt_nodent -> "" ~}) / [^%nl\])+
        ~} / text_interpolation)*
    |} ((!.) / (&(%nl+ !%gt_nodent)) / (("" -> "Error while parsing Text") => error))
text_interpolation:
    "\" (variable / inline_list / inline_dict / inline_text / ("(" %ws* (inline_functioncall / inline_expression) %ws* ")"))

number (Number): (("-"? (([0-9]+ "." [0-9]+) / ("." [0-9]+) / ([0-9]+)))-> tonumber)

-- Variables can be nameless (i.e. just %) and can't contain operators like apostrophe
-- which is a hack to allow %'s to parse as "%" and "' s" separately
variable (Var): "%" { plain_word? }

inline_list (List):
    !('[..]')
    "[" %ws* {| (inline_list_item (comma inline_list_item)* comma?)? |} %ws* "]"
indented_list (List):
    "[..]" indent {|
        list_line (nodent list_line)*
    |}
    (dedent / (("" -> "Error while parsing list") => error))
list_line:
     ((functioncall / expression) !comma)
     / (inline_list_item (comma list_line?)?)
inline_list_item: inline_functioncall / inline_expression

inline_dict (Dict):
    !('{..}')
    "{" %ws* {| (inline_dict_item (comma inline_dict_item)* comma?)? |} %ws* "}"
indented_dict (Dict):
    "{..}" indent {|
        dict_line (nodent dict_line)*
    |}
    (dedent / (("" -> "Error while parsing dict") => error))
dict_line:
    ({| {:dict_key: inline_expression / word :} %ws* ":" %ws* {:dict_value: functioncall / expression :} |} !comma)
    / (inline_dict_item (comma dict_line?)?)
inline_dict_item:
    {| {:dict_key: inline_expression / word :} %ws* ":" %ws* {:dict_value: inline_functioncall / inline_expression :} |}

block_comment(Comment): "#.." { [^%nl]* (%nl %gt_nodent [^%nl]*)* }
line_comment(Comment): "#" { [^%nl]* }

eol: %ws* line_comment? (!. / &%nl)
ignored_line: (%nodent (block_comment / line_comment)) / (%ws* (!. / &%nl))
indent: eol (%nl ignored_line)* %nl %indent ((block_comment/line_comment) (%nl ignored_line)* nodent)?
nodent: eol (%nl ignored_line)* %nl %nodent
dedent: eol (%nl ignored_line)* (((!.) &%dedent) / (&(%nl %dedent)))
comma: %ws* "," %ws*
dotdot: nodent ".." %ws*
plain_word: ([a-zA-Z0-9_] / %utf8_char)+