46845
|
1 |
(* ML-Yacc Parser Generator (c) 1989 Andrew W. Appel, David R. Tarditi *)
|
|
2 |
|
|
3 |
(* functor Join creates a user parser by putting together a Lexer structure,
|
|
4 |
an LrValues structure, and a polymorphic parser structure. Note that
|
|
5 |
the Lexer and LrValues structure must share the type pos (i.e. the type
|
|
6 |
of line numbers), the type svalues for semantic values, and the type
|
|
7 |
of tokens.
|
|
8 |
*)
|
|
9 |
|
|
10 |
functor Join(structure Lex : LEXER
|
|
11 |
structure ParserData: PARSER_DATA
|
|
12 |
structure LrParser : LR_PARSER
|
|
13 |
sharing ParserData.LrTable = LrParser.LrTable
|
|
14 |
sharing ParserData.Token = LrParser.Token
|
|
15 |
sharing type Lex.UserDeclarations.svalue = ParserData.svalue
|
|
16 |
sharing type Lex.UserDeclarations.pos = ParserData.pos
|
|
17 |
sharing type Lex.UserDeclarations.token = ParserData.Token.token)
|
|
18 |
: PARSER =
|
|
19 |
struct
|
|
20 |
structure Token = ParserData.Token
|
|
21 |
structure Stream = LrParser.Stream
|
|
22 |
|
|
23 |
exception ParseError = LrParser.ParseError
|
|
24 |
|
|
25 |
type arg = ParserData.arg
|
|
26 |
type pos = ParserData.pos
|
|
27 |
type result = ParserData.result
|
|
28 |
type svalue = ParserData.svalue
|
|
29 |
val makeLexer = LrParser.Stream.streamify o Lex.makeLexer
|
|
30 |
val parse = fn (lookahead,lexer,error,arg) =>
|
|
31 |
(fn (a,b) => (ParserData.Actions.extract a,b))
|
|
32 |
(LrParser.parse {table = ParserData.table,
|
|
33 |
lexer=lexer,
|
|
34 |
lookahead=lookahead,
|
|
35 |
saction = ParserData.Actions.actions,
|
|
36 |
arg=arg,
|
|
37 |
void= ParserData.Actions.void,
|
|
38 |
ec = {is_keyword = ParserData.EC.is_keyword,
|
|
39 |
noShift = ParserData.EC.noShift,
|
|
40 |
preferred_change = ParserData.EC.preferred_change,
|
|
41 |
errtermvalue = ParserData.EC.errtermvalue,
|
|
42 |
error=error,
|
|
43 |
showTerminal = ParserData.EC.showTerminal,
|
|
44 |
terms = ParserData.EC.terms}}
|
|
45 |
)
|
|
46 |
val sameToken = Token.sameToken
|
|
47 |
end
|
|
48 |
|
|
49 |
(* functor JoinWithArg creates a variant of the parser structure produced
|
|
50 |
above. In this case, the makeLexer take an additional argument before
|
|
51 |
yielding a value of type unit -> (svalue,pos) token
|
|
52 |
*)
|
|
53 |
|
|
54 |
functor JoinWithArg(structure Lex : ARG_LEXER
|
|
55 |
structure ParserData: PARSER_DATA
|
|
56 |
structure LrParser : LR_PARSER
|
|
57 |
sharing ParserData.LrTable = LrParser.LrTable
|
|
58 |
sharing ParserData.Token = LrParser.Token
|
|
59 |
sharing type Lex.UserDeclarations.svalue = ParserData.svalue
|
|
60 |
sharing type Lex.UserDeclarations.pos = ParserData.pos
|
|
61 |
sharing type Lex.UserDeclarations.token = ParserData.Token.token)
|
|
62 |
: ARG_PARSER =
|
|
63 |
struct
|
|
64 |
structure Token = ParserData.Token
|
|
65 |
structure Stream = LrParser.Stream
|
|
66 |
|
|
67 |
exception ParseError = LrParser.ParseError
|
|
68 |
|
|
69 |
type arg = ParserData.arg
|
|
70 |
type lexarg = Lex.UserDeclarations.arg
|
|
71 |
type pos = ParserData.pos
|
|
72 |
type result = ParserData.result
|
|
73 |
type svalue = ParserData.svalue
|
|
74 |
|
|
75 |
val makeLexer = fn s => fn arg =>
|
|
76 |
LrParser.Stream.streamify (Lex.makeLexer s arg)
|
|
77 |
val parse = fn (lookahead,lexer,error,arg) =>
|
|
78 |
(fn (a,b) => (ParserData.Actions.extract a,b))
|
|
79 |
(LrParser.parse {table = ParserData.table,
|
|
80 |
lexer=lexer,
|
|
81 |
lookahead=lookahead,
|
|
82 |
saction = ParserData.Actions.actions,
|
|
83 |
arg=arg,
|
|
84 |
void= ParserData.Actions.void,
|
|
85 |
ec = {is_keyword = ParserData.EC.is_keyword,
|
|
86 |
noShift = ParserData.EC.noShift,
|
|
87 |
preferred_change = ParserData.EC.preferred_change,
|
|
88 |
errtermvalue = ParserData.EC.errtermvalue,
|
|
89 |
error=error,
|
|
90 |
showTerminal = ParserData.EC.showTerminal,
|
|
91 |
terms = ParserData.EC.terms}}
|
|
92 |
)
|
|
93 |
val sameToken = Token.sameToken
|
|
94 |
end;
|