Advertisement
logicmoo

Diginet's code

Apr 20th, 2017
285
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Prolog 2.42 KB | None | 0 0
  1. % lexer.pro
  2. :- use_module(library(pure_input)).
  3.  
  4. phrase_test(Grammar,Str) :- string_codes(Str,Codes),phrase(Grammar,Codes).
  5.  
  6. tokenize(File,Z) :-
  7.  setup_call_cleanup(
  8.    open(File,read,Stream),
  9.    stream_to_lazy_list(Stream,List),
  10.    phrase(tokens(Z),List),
  11.    close(Stream)),!.
  12.  
  13.  
  14. string([H|T]) --> {code_type(H,alpha)},[H],(string(T); ([],{T=[]})).
  15. number([H|T]) --> {code_type(H,digit)},[H],(number(T); ([],{T=[]})).
  16.  
  17. left_quote([_|T]) --> `\``,(left_quote(T); ([],{T=[]})).
  18. right_quote([_|T]) --> `'`,(right_quote(T); ([],{T=[]})).
  19.  
  20. reserved("Program").
  21. reserved("Use").
  22. reserved("Const").
  23. reserved("Type").
  24. reserved("Var").
  25. reserved("Code").
  26. reserved("End").
  27.  
  28.  
  29. % Strip spaces, tabs and newlines.
  30. tokens([]) --> [C],{var(C)},!.  
  31. tokens(HT) --> [C], {code_type(C,white)},!,tokens(HT).
  32. tokens([H|T]) --> tokenZ(H),tokens(T).
  33. % Anything not matched counted as error
  34. tokens(syntax_err(X,Line,LinePos)) --> [C],lazy_list_location(file(_, Line, LinePos,_)), {name(X, [C])}.
  35. tokens([]) --> [].                                  
  36.  
  37. tokenZ(Make)--> did_token(Match,Make),Match,!.
  38. tokenZ(integer(N)) --> number(W), {number_codes(N,W)}.
  39. % Delimiters
  40. % tokenZ(quote("left",N)) --> left_quote(W), {length(W,N)}.
  41. % tokenZ(quote("right",N),Str) -->  right_quote(W), {length(W,N)}.
  42. tokenZ(Z) -->
  43.   lazy_list_location(file(_, Line, LinePos,_)),
  44.  string(W),
  45.   {string_codes(S,W),
  46.    (reserved(S)->
  47.     Z = keyword(S,Line:LinePos) ;
  48.     Z = ident(S,Line:LinePos))}.
  49.  
  50.  
  51.  
  52.  
  53.  
  54. did_token(`{`,delimiter("brace","left")).
  55. did_token(`}`,delimiter("brace","right")).
  56. did_token(`(`,delimiter("paren","left")).
  57. did_token(`)`,delimiter("paren","right")).
  58.  
  59.  
  60. % Structural
  61. did_token(`:`,punct("colon")).
  62. did_token(`;`,punct("semicolon")).
  63. did_token(`,`,punct("comma")).
  64. did_token(`#`,punct("hash")).
  65.  
  66. % declare new type constructors, using previously designed types
  67. % OCaml paramterized modules
  68.  
  69. % Assignment Operator
  70. did_token(`:=`,assignment).
  71. did_token(`<=`,param_assignment).
  72.  
  73. % Comparison Operators
  74. did_token(`==`,equal).
  75. did_token(`=/`,not_equal).
  76. did_token(`>=`,greater_equal).
  77. did_token(`=<`,less_equal).
  78. did_token(`>`,greater_than).
  79. did_token(`<`,less_than).
  80.  
  81. did_token(`=`,equal_sign).
  82. did_token(`.`,dot).
  83.  
  84. % Arithmetic Operators
  85. did_token(`+`,operator("add")).
  86. did_token(`-`,operator("subtract")).
  87. did_token(`*`,operator("multiply")).
  88. did_token(`/`,operator("divide")).
  89. did_token(`^`,operator("power")).
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement