| 1 |
(* grammar-syntax.sml |
| 2 |
* |
| 3 |
* COPYRIGHT (c) 2009 The Fellowship of SML/NJ (http://www.smlnj.org) |
| 4 |
* All rights reserved. |
| 5 |
* |
| 6 |
* Parse tree for grammar input. |
| 7 |
*) |
| 8 |
|
| 9 |
structure GrammarSyntax = |
| 10 |
struct |
| 11 |
|
| 12 |
type span = Err.span |
| 13 |
type code = span * String.string |
| 14 |
type symbol = Atom.atom |
| 15 |
type name = string |
| 16 |
type ty = string |
| 17 |
type constr = (symbol * ty option * Atom.atom option) |
| 18 |
|
| 19 |
datatype decl |
| 20 |
= NAME of name |
| 21 |
| HEADER of code |
| 22 |
| START of symbol |
| 23 |
| ENTRY of symbol |
| 24 |
| KEYWORD of symbol (* %keywords TOKEN ... *) |
| 25 |
| VALUE of symbol * code (* %value TOKEN ( ... ) *) |
| 26 |
| PREFER of symbol (* %prefer TOKEN ... *) |
| 27 |
| CHANGE of (symbol list * symbol list) (* %change TOKEN* -> TOKEN* | ... *) |
| 28 |
| DEFS of code |
| 29 |
| TOKEN of constr |
| 30 |
| TOKENTYPE of ty (* %tokentype monotype *) |
| 31 |
| IMPORT of { |
| 32 |
filename : string, |
| 33 |
dropping : (span * symbol) list |
| 34 |
} |
| 35 |
| REFCELL of name * ty * code |
| 36 |
| RULE of { |
| 37 |
lhs : symbol, |
| 38 |
formals : name list, |
| 39 |
rhs : rhs |
| 40 |
} |
| 41 |
| NONTERM of symbol * ty |
| 42 |
|
| 43 |
and rhs = RHS of { |
| 44 |
items : (string option * (span * item)) list, |
| 45 |
try : bool, |
| 46 |
predicate : code option, |
| 47 |
action : code option, |
| 48 |
loc : span |
| 49 |
} |
| 50 |
|
| 51 |
and item |
| 52 |
= SYMBOL of symbol * code option |
| 53 |
| SUBRULE of rhs list (* ( ... ) *) |
| 54 |
| CLOS of span * item (* ( ... )* *) |
| 55 |
| POSCLOS of span * item (* ( ... )+ *) |
| 56 |
| OPT of span * item (* ( ... )? *) |
| 57 |
|
| 58 |
type grammar = (span * decl) list |
| 59 |
|
| 60 |
local |
| 61 |
fun ppDecl (_, NAME n) = "%name" |
| 62 |
| ppDecl (_, HEADER _) = "%code" |
| 63 |
| ppDecl (_, START s) = "%start" |
| 64 |
| ppDecl (_, ENTRY s) = "%entry" |
| 65 |
| ppDecl (_, KEYWORD s) = "%keyword" |
| 66 |
| ppDecl (_, VALUE _) = "%value" |
| 67 |
| ppDecl (_, PREFER _) = "%prefer" |
| 68 |
| ppDecl (_, CHANGE _) = "%change" |
| 69 |
| ppDecl (_, DEFS c) = "%defs" |
| 70 |
| ppDecl (_, TOKENTYPE c) = "%tokentype" |
| 71 |
| ppDecl (_, TOKEN cstr) = "%tokens" |
| 72 |
| ppDecl (_, NONTERM cstr) = "%nonterm" |
| 73 |
| ppDecl (_, IMPORT {filename, dropping}) = "%import" |
| 74 |
| ppDecl (_, REFCELL (n, ty, c)) = "%refcell" |
| 75 |
| ppDecl (_, RULE {lhs, formals, rhs}) = "-- rule: " ^ (Atom.toString lhs) |
| 76 |
in |
| 77 |
fun ppGrammar decls = String.concatWith "\n" (map ppDecl decls) |
| 78 |
end |
| 79 |
|
| 80 |
end |