/
smlpeg.peg
124 lines (111 loc) · 3.79 KB
/
smlpeg.peg
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
(*******************************************************************************
* The Kuru Programming Language Compiler Toolset (http://www.kuru-lang.org)
* Copyright (C) 2010 Gian Perrone
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*******************************************************************************
* This file:
* Definition of the grammar for KuPEG grammar files.
*
* Based on peg-bootstrap by Kragen Javier Sitaker
* http://github.com/kragen/peg-bootstrap
* Ported to Kuru (with new primitives and syntax) by Gian Perrone
******************************************************************************)
fun kupeg_join l = String.concatWith "" l
%name KupegParser
%start sentence
datatype ast =
Rule of string * ast
| Choice of ast * ast
| Sequence of ast * ast
| Literal of string
| Nonterm of string
| Negation of ast
| Result of string
| Label of string * ast
| Star of ast
| Plus of ast
| Null
%nonterm sp of string
%nonterm _ of string
%nonterm rule of ast
%nonterm sentence of ast list
%nonterm meta of string
%nonterm name of string
%nonterm namechar of string
%nonterm termfrag of ast
%nonterm term of ast
%nonterm nonterminal of ast
%nonterm labeled of ast
%nonterm sequence of ast
%nonterm string of ast
%nonterm stringcontents of string
%nonterm choice of ast
%nonterm negation of ast
%nonterm result_expression of ast
%nonterm expr of string
%nonterm exprcontents of string
%nonterm parenthesized of ast
val kplineNum = ref 0
val kpfileName = ref "__dummy__"
%%
sp <- " " / "\n" / "\t".
_ <- sp _ / .
rule <- n: name _ "<-"_ body: choice "."_ ->
((Rule($n,$body)))
.
sentence <- _ r: rule g: sentence -> ($r :: $g)
/ _ r: rule -> ($r :: [])
.
meta <- "!" / "\"" / "<-" / "/" / "." / "(" / ")" /
":" / "->" / "*" / "+".
name <- c: namechar n: name -> ($c ^ $n) / namechar.
namechar <- !meta !sp char.
termfrag <- labeled / nonterminal / string / negation / parenthesized.
term <- t: termfrag "*" -> (Star($t))
/ t: termfrag "+" -> (Plus($t))
/ t: termfrag -> ($t).
nonterminal <- n: name _ ->
(Nonterm ($n))
.
labeled <- label: name _ ":"_ value: term ->
(Label($label,$value))
.
sequence <- foo: term bar: sequence ->
(Sequence($foo, $bar))
/ result_expression
/ -> (Null)
.
string <- "\"" s: stringcontents "\""_ ->
(Literal ($s))
.
stringcontents <- !"\\" !"\"" c: char s: stringcontents ->
($c ^ $s)
/ b: "\\" c: char s: stringcontents ->
($b ^ $c ^ $s)
/ -> ("").
choice <- a: sequence "/"_ b: choice ->
(Choice($a,$b))
/ sequence.
negation <- "!"_ t: term ->
(Negation ($t))
.
result_expression <- "->"_ result: expr _ ->
(Result ($result))
.
expr <- "("_ e: exprcontents ")" -> ("(" ^ $e ^ ")").
exprcontents <- c: (!"(" !")" char / expr) e: exprcontents ->
($c ^ $e)
/ -> ("").
parenthesized <- "("_ body: choice ")"_ -> (($body)).