home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 7 / Apprentice-Release7.iso / Source Code / C / Applications / Moscow ML 1.42 / src / lex / Scanner.lex < prev    next >
Encoding:
Text File  |  1997-08-18  |  3.8 KB  |  139 lines  |  [TEXT/R*ch]

  1. (* The lexical analyzer for lexer definitions. Bootstrapped! *)
  2.  
  3. {
  4. open Fnlib Syntax Grammar Scan_aux;
  5. }
  6.  
  7. rule main = parse
  8.     [` ` `\n` `\r` `\t` ] +
  9.     { main lexbuf }
  10.   | "(*"
  11.     { (comment_depth := 1; comment lexbuf; main lexbuf) }
  12.   | "*)"
  13.       { raise Lexical_error "unmatched comment bracket" }
  14.   | [`A`-`Z` `a`-`z`] ( [ `A`-`Z` `a`-`z` `0`-`9` `_` `'`] )*
  15.     { case getLexeme lexbuf of
  16.         "rule" => Trule
  17.       | "parse" => Tparse
  18.       | "and" => Tand
  19.       | "eof" => Teof
  20.       | s => Tident s }
  21.   | `"`
  22.     { (reset_string_buffer();
  23.        string lexbuf;
  24.        Tstring(get_stored_string())) }
  25.   | "`"
  26.     { Tchar(char lexbuf) }
  27.   | `{`
  28.     { let val n1 = getLexemeEnd lexbuf
  29.           val () = brace_depth := 1
  30.           val n2 = action lexbuf
  31.       in Taction(Location(n1, n2)) end }
  32.   | `=`  { Tequal }
  33.   | ";"  { Tend }
  34.   | `|`  { Tor }
  35.   | `_`  { Tunderscore }
  36.   | "eof"  { Teof }
  37.   | `[`  { Tlbracket }
  38.   | `]`  { Trbracket }
  39.   | `*`  { Tstar }
  40.   | `?`  { Tmaybe }
  41.   | `+`  { Tplus }
  42.   | `(`  { Tlparen }
  43.   | `)`  { Trparen }
  44.   | `^`  { Tcaret }
  45.   | `-`  { Tdash }
  46.   | (eof | `\026`)
  47.     { raise Lexical_error "unterminated lexer definition" }
  48.   | _
  49.     { raise Lexical_error
  50.              ("illegal character #" ^ makestring(getLexeme lexbuf)) }
  51.  
  52. and action = parse
  53.     `{`
  54.         { (incr brace_depth; action lexbuf) }
  55.   | `}`
  56.         { (decr brace_depth;
  57.            if !brace_depth = 0 then
  58.              getLexemeStart lexbuf
  59.            else
  60.              action lexbuf) }
  61.   | `"`
  62.     { (reset_string_buffer();
  63.        string lexbuf;
  64.        reset_string_buffer();
  65.        action lexbuf) }
  66.   | "(*"
  67.     { (comment_depth := 1; comment lexbuf; action lexbuf) }
  68.   | "*)"
  69.       { raise Lexical_error "unmatched comment bracket" }
  70.   | (eof | `\026`)
  71.     { raise Lexical_error "unterminated action" }
  72.   | _
  73.     { action lexbuf }
  74.  
  75. and string = parse
  76.     `"`
  77.     { () }
  78.   | `\\` [` ` `\t` `\n` `\r`]+ `\\`
  79.       { string lexbuf }
  80.   | `\\` [`\\` `"` `n` `t` `b` `r`]
  81.     { (store_string_char(char_for_backslash(getLexemeChar lexbuf 1));
  82.        string lexbuf) }
  83.   | `\\` `^` [`@`-`_`]
  84.       { (store_string_char(
  85.            Char.chr(Char.ord(getLexemeChar lexbuf 2) - 64));
  86.          string lexbuf) }
  87.   | `\\` [`0`-`9`] [`0`-`9`] [`0`-`9`]
  88.     { let val code = char_for_decimal_code lexbuf 1 in
  89.         if Char.ord code >= 256 then
  90.           raise Lexical_error "character code in string > 255"
  91.         else ();
  92.         store_string_char code;
  93.         string lexbuf
  94.       end }
  95.   | `\\`
  96.       { raise Lexical_error "ill-formed escape sequence in string" }
  97.   | (eof | `\026`)
  98.     { raise Lexical_error "unterminated string" }
  99.   | [`\001`-`\031` `\127` `\255`]
  100.       { raise Lexical_error "invalid character in string" }
  101.   | _
  102.     { (store_string_char(getLexemeChar lexbuf 0);
  103.        string lexbuf) }
  104.  
  105. and char = parse
  106.     `\\` [`\\` `\`` `n` `t` `b` `r`] "`"
  107.     { char_for_backslash (getLexemeChar lexbuf 1) }
  108.   | `\\` `^` [`@`-`_`] "`"
  109.       { Char.chr(Char.ord(getLexemeChar lexbuf 2) - 64) }
  110.   | `\\` [`0`-`9`] [`0`-`9`] [`0`-`9`] "`"
  111.     { let val code = char_for_decimal_code lexbuf 1 in
  112.         if Char.ord code >= 256 then
  113.           raise Lexical_error "character code in string > 255"
  114.         else ();
  115.         code
  116.       end }
  117.   | `\\`
  118.       { raise Lexical_error "ill-formed escape sequence in character constant" }
  119.   | (eof | `\026`)
  120.     { raise Lexical_error "unterminated character constant" }
  121.   | [`\001`-`\031` `\127` `\255`]
  122.       { raise Lexical_error "invalid character in character constant" }
  123.   | _ "`"
  124.     { getLexemeChar lexbuf 0 }
  125.   | _
  126.     { raise Lexical_error "ill-formed character constant" }
  127.  
  128. and comment = parse
  129.     "(*"
  130.     { (incr comment_depth; comment lexbuf) }
  131.   | "*)"
  132.     { (decr comment_depth;
  133.        if !comment_depth = 0 then () else comment lexbuf) }
  134.   | (eof | `\026`)
  135.     { raise Lexical_error "unterminated comment" }
  136.   | _
  137.     { comment lexbuf }
  138. ;
  139.