Advertisement
Guest User

Ramon Snir

a guest
Sep 6th, 2010
190
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.58 KB | None | 0 0
  1. {
  2. module Lexers.English
  3.  
  4. open Core
  5. open Core.Parser
  6. open System
  7. open System.Text
  8. open Microsoft.FSharp.Text.Lexing
  9.  
  10. let keywords =
  11. [
  12. "new", NEW;
  13. "if", IF;
  14. "then", THEN;
  15. "else", ELSE;
  16. "endif", ENDIF;
  17. "for", FOR;
  18. "to", TO;
  19. "inc", INC;
  20. "do", DO;
  21. "while", WHILE;
  22. "next", NEXT;
  23. "define", DEFINE;
  24. "as", AS;
  25. "enddefine", ENDDEFINE;
  26. "ctor", CTOR
  27. ] |> Map.ofList
  28.  
  29. let ops =
  30. [
  31. ")", RP;
  32. "(", LP;
  33. "<=", ASSIGN;
  34. ",", COMMA;
  35. "#", HASH
  36. ] |> Map.ofList
  37. }
  38.  
  39. let number = ['+''-']?['0'-'9']+('.'['0'-'9']*)?
  40. let string = ("@\""[^'"']*'"'|'"'(('\\''"')|[^'"'])*'"')
  41. let id = [\w'.']+
  42. let op = [\W]+
  43. let use = "#use "[^'\n''\r']
  44.  
  45. let whitespace = [' ' '\t']+
  46. let newline = ('\n' | '\r' '\n')
  47.  
  48. rule tokenize = parse
  49. | whitespace { tokenize lexbuf }
  50. | newline { NL }
  51. | eof { EOF }
  52. | id {
  53. let text = Encoding.UTF8.GetString(lexbuf.Lexeme)
  54. if Map.containsKey text keywords then
  55. keywords.[text]
  56. else
  57. ID <| Encoding.UTF8.GetString(lexbuf.Lexeme)
  58. }
  59. | string { STRING <| Encoding.UTF8.GetString(lexbuf.Lexeme) }
  60. | op {
  61. let text = Encoding.UTF8.GetString(lexbuf.Lexeme)
  62. if Map.containsKey text ops then
  63. ops.[text]
  64. else
  65. OP <| Encoding.UTF8.GetString(lexbuf.Lexeme).Trim()
  66. }
  67. | use { USE <| Encoding.UTF8.GetString(lexbuf.Lexeme).Substring(5) }
  68. | number { NUMBER <| Convert.ToDouble(Encoding.UTF8.GetString(lexbuf.Lexeme)) }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement