| 1 |
(* spec.grm |
| 2 |
* |
| 3 |
* COPYRIGHT (c) 2009 The Fellowship of SML/NJ (http://www.smlnj.org) |
| 4 |
* All rights reserved. |
| 5 |
* |
| 6 |
* ML-Antlr grammar for ML-Antlr. |
| 7 |
*) |
| 8 |
|
| 9 |
%defs ( |
| 10 |
structure GS = GrammarSyntax |
| 11 |
structure StreamPos = AntlrStreamPos |
| 12 |
|
| 13 |
fun lift f (vspan, v) = (vspan, f v) |
| 14 |
fun mapFst f (fst, snd) = (f fst, snd) |
| 15 |
|
| 16 |
val trimQuotes = |
| 17 |
Substring.string o |
| 18 |
(Substring.triml 1) o |
| 19 |
(Substring.trimr 1) o |
| 20 |
Substring.full |
| 21 |
|
| 22 |
); |
| 23 |
|
| 24 |
%name Spec; |
| 25 |
|
| 26 |
%tokens |
| 27 |
: KW_change ("%change") |
| 28 |
| KW_defs ("%defs") |
| 29 |
| KW_dropping ("%dropping") |
| 30 |
| KW_entry ("%entry") |
| 31 |
| KW_header ("%header") |
| 32 |
| KW_import ("%import") |
| 33 |
| KW_keywords ("%keywords") |
| 34 |
| KW_name ("%name") |
| 35 |
| KW_nonterms ("%nonterms") |
| 36 |
| KW_prefer ("%prefer") |
| 37 |
| KW_refcell ("%refcell") |
| 38 |
| KW_start ("%start") |
| 39 |
| KW_tokens ("%tokens") |
| 40 |
| KW_tokentype ("%tokentype") |
| 41 |
| KW_try ("%try") |
| 42 |
| KW_value ("%value") |
| 43 |
| KW_where ("%where") |
| 44 |
| LP ("(") |
| 45 |
| RP (")") |
| 46 |
| LSB ("[") |
| 47 |
| RSB ("]") |
| 48 |
| LCB ("{") |
| 49 |
| RCB ("}") |
| 50 |
| SLASH ("/") |
| 51 |
| COLON (":") |
| 52 |
| SEMI (";") |
| 53 |
| COMMA (",") |
| 54 |
| AT ("@") |
| 55 |
| DOLLAR ("$") |
| 56 |
| BAR ("|") |
| 57 |
| STAR ("*") |
| 58 |
| PLUS ("+") |
| 59 |
| QUERY ("?") |
| 60 |
| EQ ("=") |
| 61 |
| ARROW ("->") |
| 62 |
| DARROW ("=>") |
| 63 |
| REFSET (":=") |
| 64 |
| OF ("of") |
| 65 |
| CODE of string |
| 66 |
| ID of string |
| 67 |
| STRING of string |
| 68 |
| IDDOT of string (* identifier with dot (e.g., "module.") *) |
| 69 |
| TYVAR of string (* ML-style type variable name *) |
| 70 |
| INT of string (* integer label within record *) |
| 71 |
| BOGUS |
| 72 |
; |
| 73 |
|
| 74 |
%refcell liftSpan : AntlrStreamPos.span -> GrammarSyntax.span |
| 75 |
= (fn _ => let val z = ("", 0, 0) in (z, z) end); |
| 76 |
|
| 77 |
%nonterm File of GS.grammar ; |
| 78 |
File(fileName, sm) |
| 79 |
: ( => (liftSpan := (fn (p1, p2) => ((fileName, StreamPos.lineNo sm p1, |
| 80 |
StreamPos.colNo sm p1), |
| 81 |
(fileName, StreamPos.lineNo sm p2, |
| 82 |
StreamPos.colNo sm p2))) |
| 83 |
) ) |
| 84 |
(Decl ";")* => (map (mapFst (!liftSpan)) (List.concat SR2)) |
| 85 |
; |
| 86 |
|
| 87 |
%nonterm Decl of (StreamPos.span * GS.decl) list ; |
| 88 |
Decl |
| 89 |
: "%name" ID |
| 90 |
=> ([ (FULL_SPAN, GS.NAME ID) ]) |
| 91 |
| "%header" Code |
| 92 |
=> ([ (FULL_SPAN, GS.HEADER Code) ]) |
| 93 |
| "%start" ID |
| 94 |
=> ([ (FULL_SPAN, GS.START (Atom.atom ID)) ]) |
| 95 |
| "%entry" IDList |
| 96 |
=> (map (lift (GS.ENTRY o Atom.atom)) IDList) |
| 97 |
| "%keywords" SymList |
| 98 |
=> (map (lift GS.KEYWORD) SymList) |
| 99 |
| "%value" ID Code |
| 100 |
=> ([ (FULL_SPAN, GS.VALUE (Atom.atom ID, Code)) ]) |
| 101 |
| "%prefer" SymList |
| 102 |
=> (map (lift GS.PREFER) SymList) |
| 103 |
| "%change" ChangeList |
| 104 |
=> (map (lift GS.CHANGE) ChangeList) |
| 105 |
| "%defs" Code |
| 106 |
=> ([ (FULL_SPAN, GS.DEFS Code) ]) |
| 107 |
| "%tokentype" QualID |
| 108 |
=> ([ (FULL_SPAN, GS.TOKENTYPE QualID)]) |
| 109 |
| "%tokens" ":"? ConstrList |
| 110 |
=> (List.map (lift GS.TOKEN) ConstrList) |
| 111 |
| "%import" STRING ("%dropping" SymList)? |
| 112 |
=> ([ (FULL_SPAN, GS.IMPORT{ |
| 113 |
filename = trimQuotes STRING, |
| 114 |
dropping = map (mapFst (!liftSpan)) (getOpt (SR, [])) |
| 115 |
}) ]) |
| 116 |
| "%refcell" ID ":" Ty "=" Code |
| 117 |
=> ([ (FULL_SPAN, GS.REFCELL (ID, Ty, Code)) ]) |
| 118 |
| "%nonterms" ":"? TyAnn ("|" TyAnn)* |
| 119 |
=> (map (lift GS.NONTERM) (TyAnn::SR)) |
| 120 |
| ID Formals? ":" AltList |
| 121 |
=> (map (fn (span, alt) => |
| 122 |
(span, GS.RULE { |
| 123 |
lhs = Atom.atom ID, |
| 124 |
formals = getOpt(Formals, []), |
| 125 |
rhs = alt |
| 126 |
})) AltList) |
| 127 |
; |
| 128 |
|
| 129 |
Formals : "(" ID ("," ID)* ")" => (ID::SR) ; |
| 130 |
IDList : ID' ("," ID')* => (ID'::SR) ; |
| 131 |
SymList : Symbol' ("," Symbol')* => (Symbol'::SR) ; |
| 132 |
|
| 133 |
ChangeList |
| 134 |
: Change ("|" Change)* |
| 135 |
=> (Change::SR) |
| 136 |
; |
| 137 |
|
| 138 |
Change |
| 139 |
: Symbol* "->" Symbol* |
| 140 |
=> (FULL_SPAN, (Symbol1, Symbol2)) |
| 141 |
; |
| 142 |
|
| 143 |
AltList : Alt ("|" Alt)* => (Alt::SR) ; |
| 144 |
TyAnn : ID ("of" Ty | => ("unit")) => (FULL_SPAN, (Atom.atom ID, SR)) ; |
| 145 |
|
| 146 |
Alt : "%try"? NamedItem* ("%where" Code)? ("=>" Code)? |
| 147 |
=> (FULL_SPAN, GS.RHS { |
| 148 |
items = NamedItem, |
| 149 |
try = isSome KW_try, |
| 150 |
predicate = SR1, |
| 151 |
action = SR2, |
| 152 |
loc = (!liftSpan) FULL_SPAN |
| 153 |
}) |
| 154 |
; |
| 155 |
|
| 156 |
NamedItem |
| 157 |
: (ID "=")? Item => (SR, Item) ; |
| 158 |
|
| 159 |
Item |
| 160 |
: PrimItem |
| 161 |
( "*" => ((!liftSpan) PrimItem_SPAN, GS.CLOS PrimItem) |
| 162 |
| "+" => ((!liftSpan) PrimItem_SPAN, GS.POSCLOS PrimItem) |
| 163 |
| "?" => ((!liftSpan) PrimItem_SPAN, GS.OPT PrimItem) |
| 164 |
| => (PrimItem) ) |
| 165 |
=> (SR) |
| 166 |
; |
| 167 |
|
| 168 |
PrimItem |
| 169 |
: Symbol ("@" Code)? |
| 170 |
=> ((!liftSpan) FULL_SPAN, GS.SYMBOL (Symbol, SR)) |
| 171 |
| "(" AltList ")" |
| 172 |
=> ((!liftSpan) FULL_SPAN, GS.SUBRULE (map (fn (_, alt) => alt) AltList)) |
| 173 |
; |
| 174 |
|
| 175 |
ID' : ID => (ID_SPAN, ID) ; |
| 176 |
|
| 177 |
Symbol' |
| 178 |
: Symbol |
| 179 |
=> (FULL_SPAN, Symbol) |
| 180 |
; |
| 181 |
|
| 182 |
Symbol |
| 183 |
: ID |
| 184 |
=> (Atom.atom ID) |
| 185 |
| STRING |
| 186 |
=> (Atom.atom STRING) |
| 187 |
; |
| 188 |
|
| 189 |
Constr : ID ("of" Ty)? Abbrev? => (FULL_SPAN, (Atom.atom ID, SR, Abbrev)) ; |
| 190 |
ConstrList |
| 191 |
: Constr ("|" Constr)* => (Constr::SR) |
| 192 |
; |
| 193 |
|
| 194 |
Ty : TyFun |
| 195 |
; |
| 196 |
|
| 197 |
TyFun |
| 198 |
: TyProd ("->" TyProd)* => (String.concatWith " -> " (TyProd::SR)) ; |
| 199 |
|
| 200 |
TyProd |
| 201 |
: TyApp ("*" TyApp)* => (String.concatWith " * " (TyApp::SR)) ; |
| 202 |
|
| 203 |
TyApp |
| 204 |
: %try "(" Ty ("," Ty)+ ")" QualID |
| 205 |
=> ("(" ^ String.concatWith "," (Ty::SR) ^ ") " ^ QualID) |
| 206 |
| %try TyAtom ( QualID => (TyAtom ^ " " ^ QualID) | => (TyAtom)) |
| 207 |
=> (SR) |
| 208 |
; |
| 209 |
|
| 210 |
TyAtom |
| 211 |
: TYVAR |
| 212 |
| "(" Ty ")" => ("(" ^ Ty ^ ")") |
| 213 |
| "{" RowList "}" => ("{ " ^ RowList ^" } ") |
| 214 |
| "{" "}" => ("{}") |
| 215 |
| QualID |
| 216 |
; |
| 217 |
|
| 218 |
Row : Label ":" Ty => (Label ^ " : " ^ Ty) ; |
| 219 |
RowList : Row ("," Row)* => (String.concatWith "," (Row::SR)) ; |
| 220 |
|
| 221 |
Label : ID | INT ; |
| 222 |
|
| 223 |
QualID |
| 224 |
: ID |
| 225 |
| IDDOT QualID => (IDDOT ^ QualID) |
| 226 |
; |
| 227 |
|
| 228 |
Abbrev : "(" STRING ")" => (Atom.atom STRING) ; |
| 229 |
|
| 230 |
%nonterm Code of GrammarSyntax.code ; |
| 231 |
Code : CODE => ((!liftSpan) FULL_SPAN, CODE) ; |