type pos = int type svalue = Tok.svalue type ('a,'b) token = ('a,'b) Tok.token type lexresult= (svalue,pos) token open Tok val eof = fn () => EOF(~1,~1) val error = (* fn (e,l : int,_) => output(std_out,"line " ^ (makestring l) ^ ": " ^ e ^ "\n") *) fn _ => () (* what to do (i.e. switch start states) after recognizing an action *) val afterAction = ref (fn () => ()) (* paren counting for actions *) val pcount = ref 0 val inquote = ref false fun inc r = if !inquote then () else r := !r + 1 fun dec r = if !inquote then () else r := !r - 1 (* buffer for accumulating test across the rules for actions *) local val text = ref ([] : string list) in fun clrAction () = (text := ["("]) fun updAction str = if !pcount > 0 then (text := str :: !text) else () fun getAction () = String.concat (rev (!text)) end structure SIS = RegExp.SymSet fun uniChar s = let fun toW32 (c : Char.char) : UTF8.wchar = (case c of #"0" => 0w0 | #"1" => 0w1 | #"2" => 0w2 | #"3" => 0w3 | #"4" => 0w4 | #"5" => 0w5 | #"6" => 0w6 | #"7" => 0w7 | #"8" => 0w8 | #"9" => 0w9 | #"a" => 0w10 | #"A" => 0w10 | #"b" => 0w11 | #"B" => 0w11 | #"c" => 0w12 | #"C" => 0w12 | #"d" => 0w13 | #"D" => 0w13 | #"e" => 0w14 | #"E" => 0w14 | #"f" => 0w15 | #"F" => 0w15 | _ => raise Fail "invalid unicode escape sequence") fun iter (#"u"::_, v) = v | iter (c::cs, v) = iter (cs, 0w16*v + (toW32 c)) | iter _ = raise Fail "invalid unicode escape sequence" in iter (List.rev (String.explode s), 0w0) end val highAscii = SIS.interval(0w128, 0w255) %% %header (functor MLLexLexFun(structure Tok: MLLex_TOKENS)); %s DEFS RE RECB CHARCLASS LEXSTATES ACTION STRING; %count ws = [\ \n\t\013]; alpha = [a-zA-Z]; num = [0-9]; hex = {num} | [a-fA-F]; id = {alpha}({alpha} | {num} | "_" | "'")*; %% "%%" => (YYBEGIN DEFS; LEXMARK(!yylineno, !yylineno)); ([^%] | [^%]* % [^%])* => (DECLS(yytext, !yylineno, !yylineno)); {ws} => (lex()); "%%" => (YYBEGIN RE; LEXMARK(!yylineno, !yylineno)); "%s" => (YYBEGIN LEXSTATES; STATES(!yylineno, !yylineno)); "%header" {ws}* "(" => (clrAction(); pcount := 1; inquote := false; YYBEGIN ACTION; afterAction := (fn () => YYBEGIN DEFS); HEADER(!yylineno, !yylineno)); "%structure" => (STRUCT(!yylineno, !yylineno)); "%arg" {ws}* "(" => (clrAction(); pcount := 1; inquote := false; YYBEGIN ACTION; afterAction := (fn () => YYBEGIN DEFS); ARG(!yylineno, !yylineno)); "%count" => (COUNT(!yylineno, !yylineno)); "%reject"=> (REJECTTOK(!yylineno, !yylineno)); "%unicode" => (UNICODE(!yylineno, !yylineno)); "%full" => (FULL(!yylineno, !yylineno)); {id} => (ID(yytext, !yylineno, !yylineno)); "=" => (YYBEGIN RE; EQ(!yylineno, !yylineno)); {ws} => (lex()); "?" => (QMARK(!yylineno, !yylineno)); "*" => (STAR(!yylineno, !yylineno)); "+" => (PLUS(!yylineno, !yylineno)); "|" => (BAR(!yylineno, !yylineno)); "(" => (LP(!yylineno, !yylineno)); ")" => (RP(!yylineno, !yylineno)); "$" => (DOLLAR(!yylineno, !yylineno)); "/" => (SLASH(!yylineno, !yylineno)); "." => (DOT(!yylineno, !yylineno)); "^" => (CARAT(!yylineno, !yylineno)); "{" => (YYBEGIN RECB; lex()); "\"" => (YYBEGIN STRING; lex()); "[" => (YYBEGIN CHARCLASS; LB(!yylineno, !yylineno)); "<" => (YYBEGIN LEXSTATES; LT(!yylineno, !yylineno)); ">" => (GT(!yylineno, !yylineno)); "=>" {ws}* "(" => (clrAction(); pcount := 1; inquote := false; YYBEGIN ACTION; afterAction := (fn () => YYBEGIN RE); ARROW(!yylineno, !yylineno)); ";" => (YYBEGIN DEFS; SEMI(!yylineno, !yylineno)); {ws} => (lex()); {id} => (ID(yytext, !yylineno, !yylineno)); {num}+ => (REPS(valOf (Int.fromString yytext), !yylineno, !yylineno)); "," => (COMMA(!yylineno, !yylineno)); "}" => (YYBEGIN RE; RCB(!yylineno, !yylineno)); "-]" => (YYBEGIN RE; RBD(!yylineno, !yylineno)); "]" => (YYBEGIN RE; RB(!yylineno, !yylineno)); "-" => (DASH(!yylineno, !yylineno)); "^" => (CARAT(!yylineno, !yylineno)); "\"" => (YYBEGIN RE; lex()); "\\" ({num}{3} | [btnr] | "\\" | "\"") => (CHAR(valOf (String.fromString yytext), !yylineno, !yylineno)); "\\u"{hex}{4} => (UNICHAR(uniChar yytext, !yylineno, !yylineno)); "\\h" => (HIGH_CHAR(!yylineno, !yylineno)); "\\". => (CHAR(String.substring (yytext, 1, 1), !yylineno, !yylineno)); . => (CHAR(yytext, !yylineno, !yylineno)); {id} => (LEXSTATE(yytext, !yylineno, !yylineno)); {ws} => (lex()); "," => (COMMA(!yylineno, !yylineno)); ">" => (YYBEGIN RE; GT(!yylineno, !yylineno)); ";" => (YYBEGIN DEFS; SEMI(!yylineno, !yylineno)); ";" => (if !pcount = 0 then ((!afterAction)(); ACT(getAction(), !yylineno, !yylineno)) else (updAction ";"; lex())); "(" => (updAction "("; inc pcount; lex()); ")" => (updAction ")"; dec pcount; lex()); "\\\"" => (updAction "\\\""; lex()); "\\\\" => (updAction "\\\\"; lex()); "\\" => (updAction "\\"; lex()); "\"" => (updAction "\""; inquote := not (!inquote); lex()); [^;()\"\\]* => (updAction yytext; lex()); . => (print (concat[ "[", Int.toString (!yylineno), "] Illegal character '", String.toCString yytext, "'\n" ]); continue());