[llvm-readobj] - Implement LLVM-style dumping for .stack_sizes sections.
[llvm-complete.git] / examples / OCaml-Kaleidoscope / Chapter4 / lexer.ml
blob22a915552f035aa6520b1a6144343770c8790b2a
1 (*===----------------------------------------------------------------------===
2 * Lexer
3 *===----------------------------------------------------------------------===*)
5 let rec lex = parser
6 (* Skip any whitespace. *)
7 | [< ' (' ' | '\n' | '\r' | '\t'); stream >] -> lex stream
9 (* identifier: [a-zA-Z][a-zA-Z0-9] *)
10 | [< ' ('A' .. 'Z' | 'a' .. 'z' as c); stream >] ->
11 let buffer = Buffer.create 1 in
12 Buffer.add_char buffer c;
13 lex_ident buffer stream
15 (* number: [0-9.]+ *)
16 | [< ' ('0' .. '9' as c); stream >] ->
17 let buffer = Buffer.create 1 in
18 Buffer.add_char buffer c;
19 lex_number buffer stream
21 (* Comment until end of line. *)
22 | [< ' ('#'); stream >] ->
23 lex_comment stream
25 (* Otherwise, just return the character as its ascii value. *)
26 | [< 'c; stream >] ->
27 [< 'Token.Kwd c; lex stream >]
29 (* end of stream. *)
30 | [< >] -> [< >]
32 and lex_number buffer = parser
33 | [< ' ('0' .. '9' | '.' as c); stream >] ->
34 Buffer.add_char buffer c;
35 lex_number buffer stream
36 | [< stream=lex >] ->
37 [< 'Token.Number (float_of_string (Buffer.contents buffer)); stream >]
39 and lex_ident buffer = parser
40 | [< ' ('A' .. 'Z' | 'a' .. 'z' | '0' .. '9' as c); stream >] ->
41 Buffer.add_char buffer c;
42 lex_ident buffer stream
43 | [< stream=lex >] ->
44 match Buffer.contents buffer with
45 | "def" -> [< 'Token.Def; stream >]
46 | "extern" -> [< 'Token.Extern; stream >]
47 | id -> [< 'Token.Ident id; stream >]
49 and lex_comment = parser
50 | [< ' ('\n'); stream=lex >] -> stream
51 | [< 'c; e=lex_comment >] -> e
52 | [< >] -> [< >]