(* spec.lex * * COPYRIGHT (c) 2009 The Manticore Project (http://manticore.cs.uchicago.edu) * All rights reserved. * * (With some code borrowed from ml-yacc) *) %defs ( structure Tok = SpecTokens val comLvl : int ref = ref 0 (* nesting depth of comments *) val comStart : int ref = ref 0 (* start line of current comment *) type lex_result = Tok.token fun err (lineNo, colNo, msg) = Err.errMsg [ "Lexical error [", Int.toString lineNo, ".", Int.toString colNo, "]: ", msg ] fun eof () = ( if (!comLvl > 0) then () (* err(~1, "unclosed comment starting at line " ^ Int.toString(!comStart)) *) else (); Tok.EOF) val text : string list ref = ref [] fun addText s = (text := s::(!text)) fun clrText () = (text := []) fun getText () = concat (rev (!text)) val pcount = ref 0 (* nesting depth of parentheses in CODE *) fun inc (ri as ref i) = (ri := i+1) fun dec (ri as ref i) = (ri := i-1) ); %let eol=("\n"|"\013\n"|"\013"); %let ws=("\009"|"\011"|"\012"|" "|{eol}); %let lc=[a-z]; %let uc=[A-Z]; %let alpha=({lc}|{uc}); %let digit=[0-9]; %let int=digit*; %let idchars=({alpha}|{digit}|"_"|"'"); %let id={alpha}{idchars}*; %let qualid ={id}"."; %let tyvar="'"{idchars}*; (* the PRECODE state is used for scanning the whitespace between the precursor * of a parenthesized SML code fragment and the opening "(". On most tokens it * behaves like INITIAL so that the parser's error correction can respond. *) %states STRING COM PRECODE CODE PRECONSTR CONSTR; %name SpecLex; "of" => (YYBEGIN CONSTR; Tok.OF); {ws}+ => (skip()); {id} => (Tok.ID yytext); "%change" => (Tok.KW_change); "%defs" => (YYBEGIN PRECODE; Tok.KW_defs); "%dropping" => (Tok.KW_dropping); "%entry" => (Tok.KW_entry); "%header" => (YYBEGIN PRECODE; Tok.KW_header); "%import" => (Tok.KW_import); "%keyword"("s")? => (Tok.KW_keywords); "%name" => (Tok.KW_name); "%nonterm"("s")? => (Tok.KW_nonterms); "%prefer" => (Tok.KW_prefer); "%refcell" => (YYBEGIN CONSTR; Tok.KW_refcell); "%start" => (Tok.KW_start); "%token"("s")? => (YYBEGIN CONSTR; Tok.KW_tokens); "%tokentype" => (YYBEGIN CONSTR; Tok.KW_tokentype); "%try" => (Tok.KW_try); "%value" => (YYBEGIN PRECODE; Tok.KW_value); "%where" => (YYBEGIN PRECODE; Tok.KW_where); "|" => (Tok.BAR); "@" => (YYBEGIN PRECODE; Tok.AT); "$" => (Tok.DOLLAR); "+" => (Tok.PLUS); "*" => (Tok.STAR); "?" => (Tok.QUERY); ":" => (Tok.COLON); ";" => (Tok.SEMI); "," => (Tok.COMMA); "(" => (Tok.LP); ")" => (Tok.RP); "[" => (Tok.LSB); "]" => (Tok.RSB); "/" => (Tok.SLASH); "=" => (Tok.EQ); "->" => (Tok.ARROW); "=>" => (YYBEGIN PRECODE; Tok.DARROW); "\"" => (YYBEGIN STRING; clrText(); addText yytext; ignore(continue() before YYBEGIN INITIAL); Tok.STRING (getText())); "(*" => (comLvl := 1; comStart := !yylineno; YYBEGIN COM; ignore(continue() before YYBEGIN INITIAL); continue()); "(*" => (comLvl := 1; comStart := !yylineno; YYBEGIN COM; ignore(continue() before YYBEGIN CONSTR); continue()); "(*" => (comLvl := 1; comStart := !yylineno; YYBEGIN COM; ignore(continue() before YYBEGIN PRECODE); continue()); "(*" => (comLvl := 1; comStart := !yylineno; YYBEGIN COM; ignore(continue() before YYBEGIN CODE); continue()); "(*" => (comLvl := !comLvl+1; continue()); "*)" => (comLvl := !comLvl-1; if (!comLvl = 0) then (Tok.BOGUS) else continue()); .|{eol} => (continue()); "(" => (pcount := 1; YYBEGIN CODE; clrText(); continue()); "\"" => (YYBEGIN STRING; clrText(); addText yytext; ignore(continue() before YYBEGIN PRECODE); Tok.STRING (getText())); "(" => (addText yytext; (* NOTE: the initial "(" is consumed in the PRECODE state *) inc pcount; continue()); ")" => (dec pcount; if !pcount = 0 then (YYBEGIN INITIAL; Tok.CODE (getText())) else (addText yytext; continue())); "\"" => (addText yytext; YYBEGIN STRING; ignore(continue() before YYBEGIN CODE); continue()); [^()"]+ => (addText yytext; continue()); "\"" => (addText yytext; Tok.BOGUS); {eol} => (addText yytext; err (!yylineno, !yycolno, "unclosed string"); Tok.BOGUS); \\ => (addText yytext; continue()); \\\\ => (addText yytext; continue()); (* we apply toString to yytext below to ensure that UTF8 characters are * printed in a format that is legal SML. *) [^"\\\n\013]+ => (addText(String.toString yytext); continue()); \\\" => (addText yytext; continue()); {ws} => (continue()); "of" => (Tok.OF); {id} => (Tok.ID yytext); {tyvar} => (Tok.TYVAR yytext); {qualid}=> (Tok.IDDOT yytext); {int} => (Tok.INT yytext); "|" => (Tok.BAR); "*" => (Tok.STAR); ":" => (Tok.COLON); "," => (Tok.COMMA); ";" => (YYBEGIN INITIAL; Tok.SEMI); "(" => (Tok.LP); ")" => (Tok.RP); "{" => (Tok.LCB); "}" => (Tok.RCB); "->" => (Tok.ARROW); "\"" => (YYBEGIN STRING; clrText(); addText yytext; ignore(continue() before YYBEGIN CONSTR); Tok.STRING (getText())); "=" => (YYBEGIN PRECODE; Tok.EQ); . => (err (!yylineno, !yycolno, concat["illegal character '", String.toCString yytext, "'"]); continue());