lexer.erl 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335
  1. % Autogenerated with DRAKON Editor 1.26
  2. -module(lexer).
  3. -export([create/1]).
  4. -export([send_event/2]).
  5. -export([get_state/1]).
  6. -export([get_data/1]).
  7. -export([lex/1]).
  8. % stipan.mitkin@gmail.com
  9. % http://drakon-editor.sourceforge.net/
  10. % Copyright 2015 Stepan Mitkin
  11. % This code is PUBLIC DOMAIN.
  12. add_char(State, Char) ->
  13. % item 262
  14. {Buffer, Tokens} = State,
  15. % item 326
  16. {[Char | Buffer], Tokens}
  17. .
  18. classify(Operators, Char) ->
  19. % item 351
  20. case Char =:= $_ of true ->
  21. % item 359
  22. letter
  23. ; false ->
  24. % item 354
  25. case (Char >= $0) andalso (Char =< $9) of true ->
  26. % item 369
  27. digit
  28. ; false ->
  29. % item 370
  30. case is_whitespace(Char) of true ->
  31. % item 365
  32. whitespace
  33. ; false ->
  34. % item 361
  35. case sets:is_element(Char, Operators) of true ->
  36. % item 364
  37. operator
  38. ; false ->
  39. % item 386
  40. letter
  41. end
  42. end
  43. end
  44. end
  45. .
  46. create_identifier(State) ->
  47. % item 268
  48. create_token(State, identifier)
  49. .
  50. create_number(State) ->
  51. % item 329
  52. create_token(State, number)
  53. .
  54. create_operator(State) ->
  55. % item 330
  56. create_token(State, operator)
  57. .
  58. create_operator_list() ->
  59. % item 391
  60. OperatorList = [
  61. $!,
  62. $=,
  63. $<,
  64. $>,
  65. $-,
  66. $+,
  67. $/,
  68. $\\,
  69. $*,
  70. $%,
  71. $(,
  72. $),
  73. $[,
  74. $],
  75. ${,
  76. $},
  77. $:,
  78. $.,
  79. $;,
  80. $,,
  81. $^,
  82. $|,
  83. $&,
  84. $",
  85. $'],
  86. % item 392
  87. sets:from_list(OperatorList)
  88. .
  89. create_token(State, TokenType) ->
  90. % item 327
  91. {Buffer, Tokens} = State,
  92. % item 274
  93. case Buffer =:= [] of true ->
  94. % item 328
  95. State
  96. ; false ->
  97. % item 277
  98. Text = lists:reverse(Buffer),
  99. Token = {TokenType, Text},
  100. % item 278
  101. {[], [Token | Tokens]}
  102. end
  103. .
  104. is_long_operator(Text) ->
  105. % item 338
  106. case (((Text =:= "==") orelse (Text =:= "!=")) orelse (Text =:= "<=")) orelse (Text =:= ">=") of true ->
  107. % item 345
  108. true
  109. ; false ->
  110. % item 344
  111. false
  112. end
  113. .
  114. is_whitespace(Char) ->
  115. % item 378
  116. case (((Char =:= 9) orelse (Char =:= 32)) orelse (Char =:= 13)) orelse (Char =:= 10) of true ->
  117. % item 385
  118. true
  119. ; false ->
  120. % item 384
  121. false
  122. end
  123. .
  124. lex(Text) ->
  125. % item 253
  126. State = {[], []},
  127. % item 398
  128. Lexer = create(State),
  129. % item 313
  130. Operators = create_operator_list(),
  131. % item 256
  132. FinalLexer = scan(Operators, Lexer, Text),
  133. % item 255
  134. {_, TokensInv} = get_data(FinalLexer),
  135. % item 393
  136. lists:reverse(TokensInv)
  137. .
  138. scan(Operators, Lexer, Text) ->
  139. % item 319
  140. case Text =:= [] of true ->
  141. % item 387
  142. % item 367
  143. Message = {whitespace, 32},
  144. % item 366
  145. send_event(Lexer, Message)
  146. ; false ->
  147. % item 322
  148. [Char | Rest] = Text,
  149. % item 323
  150. Type = classify(Operators, Char),
  151. Message = {Type, Char},
  152. % item 325
  153. Lexer2 = send_event(Lexer, Message),
  154. % item 324
  155. scan(Operators, Lexer2, Rest)
  156. end
  157. .
  158. try_make_long_operator(State, Char) ->
  159. % item 332
  160. {[Old], Tokens} = State,
  161. Text = [Old, Char],
  162. % item 297
  163. case is_long_operator(Text) of true ->
  164. % item 300
  165. Token = {operator, Text},
  166. % item 397
  167. NewState = {[], [Token | Tokens]},
  168. % item 301
  169. {true, NewState}
  170. ; false ->
  171. % item 302
  172. {false, []}
  173. end
  174. .
  175. identifier(Message_, State) ->
  176. case Message_ of
  177. {whitespace, _} ->
  178. % item 65
  179. {next_state, idle, create_identifier(State)}
  180. ;
  181. {letter, Char} ->
  182. % item 242
  183. {next_state, identifier, add_char(State, Char)}
  184. ;
  185. {digit, Char} ->
  186. % item 243
  187. {next_state, identifier, add_char(State, Char)}
  188. ;
  189. {operator, Char} ->
  190. % item 219
  191. StateWithId =
  192. create_identifier(State),
  193. % item 61
  194. {next_state, operator, add_char(StateWithId, Char)}
  195. ;
  196. _ ->
  197. throw({invalid_state, "Message not supported by state 'identifier'."})
  198. end
  199. .
  200. idle(Message_, State) ->
  201. case Message_ of
  202. {whitespace, _} ->
  203. % item 211
  204. {next_state, idle, State}
  205. ;
  206. {dummy, _} ->
  207. % item 211
  208. {next_state, idle, State}
  209. ;
  210. {letter, Char} ->
  211. % item 44
  212. {next_state, identifier, add_char(State, Char)}
  213. ;
  214. {digit, Char} ->
  215. % item 240
  216. {next_state, number, add_char(State, Char)}
  217. ;
  218. {operator, Char} ->
  219. % item 241
  220. {next_state, operator, add_char(State, Char)}
  221. ;
  222. _ ->
  223. throw({invalid_state, "Message not supported by state 'idle'."})
  224. end
  225. .
  226. number(Message_, State) ->
  227. case Message_ of
  228. {whitespace, _} ->
  229. % item 82
  230. {next_state, idle, create_number(State)}
  231. ;
  232. {letter, Char} ->
  233. % item 244
  234. {next_state, number, add_char(State, Char)}
  235. ;
  236. {digit, Char} ->
  237. % item 245
  238. {next_state, number, add_char(State, Char)}
  239. ;
  240. {operator, Char} ->
  241. % item 226
  242. StateWithNum =
  243. create_number(State),
  244. % item 227
  245. {next_state, operator, add_char(StateWithNum, Char)}
  246. ;
  247. _ ->
  248. throw({invalid_state, "Message not supported by state 'number'."})
  249. end
  250. .
  251. operator(Message_, State) ->
  252. case Message_ of
  253. {whitespace, _} ->
  254. % item 229
  255. {next_state, idle, create_operator(State)}
  256. ;
  257. {letter, Char} ->
  258. % item 230
  259. StateWithOp =
  260. create_operator(State),
  261. % item 93
  262. {next_state, identifier, add_char(StateWithOp, Char)}
  263. ;
  264. {digit, Char} ->
  265. % item 231
  266. StateWithOp =
  267. create_operator(State),
  268. % item 246
  269. {next_state, number, add_char(StateWithOp, Char)}
  270. ;
  271. {operator, Char} ->
  272. % item 233
  273. {IsLong, StateWithLong} =
  274. try_make_long_operator(State, Char),
  275. % item 103
  276. case IsLong of true ->
  277. % item 234
  278. {next_state, idle, StateWithLong}
  279. ; false ->
  280. % item 235
  281. StateWithOp =
  282. create_operator(State),
  283. % item 247
  284. {next_state, operator, add_char(StateWithOp, Char)}
  285. end
  286. ;
  287. _ ->
  288. throw({invalid_state, "Message not supported by state 'operator'."})
  289. end
  290. .
  291. create(State) ->
  292. {state_machine, idle, State}.
  293. get_state(Machine) ->
  294. {state_machine, StateName, _} = Machine,
  295. StateName.
  296. get_data(Machine) ->
  297. {state_machine, _, State } = Machine,
  298. State.
  299. send_event(Machine, Event) ->
  300. {state_machine, StateName, State} = Machine,
  301. NewState =
  302. case StateName of
  303. identifier ->
  304. identifier(Event, State);
  305. idle ->
  306. idle(Event, State);
  307. number ->
  308. number(Event, State);
  309. operator ->
  310. operator(Event, State);
  311. _ ->
  312. throw({invalid_state, "Unsupported state"})
  313. end,
  314. {next_state, NextStateName, NextState} = NewState,
  315. {state_machine, NextStateName, NextState}.