-
Notifications
You must be signed in to change notification settings - Fork 0
/
parser.sml
94 lines (72 loc) · 2.86 KB
/
parser.sml
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
structure Parser =
struct
exception Error = Lexer.Error
open Syntax
type pos = int
fun lift x () = x
fun identity x = x
fun null () = []
structure Arg =
struct
type int = int
type string = string
type symbol = Symbol.symbol
val sole_ident = Symbol.fromValue
val sole_number = identity
type label = label
val ident_label = IdentLabel
val number_label = NumericLabel
type constituent = constituent
val unlabeled_item = Unlabeled
val labeled_item = Labeled
val paren_item = identity
type constituents = constituent list
val nil_constituents = null
val cons_constituents = op ::
type precedence = precedence
val empty_precedence = lift EmptyPrec
val left_precedence = PrecLeft
val right_precedence = PrecRight
val no_precedence = lift PrecNone
type production = production
fun sole_production (constituents, action, prec) = (constituents, action, prec)
type productions = production list
val nil_productions = null
val cons_productions = op ::
type directive = directive
fun name_directive str = Option (Symbol.fromValue "name", str)
fun terminal_directive (ident, prec) = Terminal (ident, NONE, prec)
fun terminal_of_directive (ident, tp, prec) = Terminal (ident, SOME tp, prec)
val nonterminal_directive = Nonterminal
val start_directive = Start
val follower_directive = Follower
type directives = directive list
val nil_directives = null
val cons_directives = op ::
datatype terminal = datatype Token.token
fun error s =
(case Stream.front s of
Stream.Nil =>
(
print "Syntax error at end of file.\n";
Error
)
| Stream.Cons ((_, pos), _) =>
(
print "Syntax error at ";
print (Int.toString pos);
print ".\n";
Error
))
end
structure StreamWithPos =
CoercedStreamable (structure Streamable = StreamStreamable
type 'a item = 'a * pos
fun coerce (x, _) = x)
structure ParseMain =
ParseMainFun
(structure Streamable = StreamWithPos
structure Arg = Arg)
fun parse s =
#1 (ParseMain.parse (Lexer.lex s))
end