| 1 |
(* action.sml |
| 2 |
* |
| 3 |
* COPYRIGHT (c) 2006 |
| 4 |
* John Reppy (http://www.cs.uchicago.edu/~jhr) |
| 5 |
* Aaron Turon (http://www.cs.uchicago.edu/~adrassi) |
| 6 |
* All rights reserved. |
| 7 |
* |
| 8 |
* Simple (opaque) encapsulation of semantic actions. |
| 9 |
*) |
| 10 |
|
| 11 |
structure Action :> |
| 12 |
sig |
| 13 |
|
| 14 |
type action |
| 15 |
|
| 16 |
val action : Err.span * string -> action |
| 17 |
val empty : action |
| 18 |
val concat : action * action -> action |
| 19 |
|
| 20 |
val toString : action -> string |
| 21 |
val name : action -> string |
| 22 |
val span : action -> Err.span |
| 23 |
val code : action -> string |
| 24 |
val same : (action * action) -> bool |
| 25 |
|
| 26 |
end = struct |
| 27 |
|
| 28 |
datatype action |
| 29 |
= ACT of { |
| 30 |
id : int, |
| 31 |
code : string, |
| 32 |
span : Err.span |
| 33 |
} |
| 34 |
|
| 35 |
local |
| 36 |
val cnt = ref 0 |
| 37 |
in |
| 38 |
fun nextId() = (cnt := !cnt + 1; !cnt) |
| 39 |
end |
| 40 |
|
| 41 |
fun action (i, s) = ACT {id = nextId(), code = s, span = i} |
| 42 |
fun toString (ACT {code, span, ...}) = code |
| 43 |
(* if span = 1 then |
| 44 |
"(*#line " ^ Int.toString span ^ ".0*)" ^ code |
| 45 |
else |
| 46 |
"(*#line " ^ Int.toString (span - 1) ^ ".0*) \n" ^ code |
| 47 |
*) |
| 48 |
|
| 49 |
fun name (ACT{id, ...}) = Int.toString id |
| 50 |
fun same (ACT{id = id1, ...}, ACT {id = id2, ...}) = (id1 = id2) |
| 51 |
|
| 52 |
fun span (ACT{span, ...}) = span |
| 53 |
fun code (ACT{code, ...}) = code |
| 54 |
|
| 55 |
val empty = action (Err.emptySpan, "") |
| 56 |
fun concat (a, b) = |
| 57 |
if same (a, empty) then b |
| 58 |
else if same (b, empty) then a |
| 59 |
else action (span a, code a ^ code b) |
| 60 |
|
| 61 |
end |