-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsamenhirLexer.mll
64 lines (61 loc) · 1.72 KB
/
samenhirLexer.mll
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
{
(*
###########################################
# #
# Lexer de Samenhir #
# #
# Il compte mal les lignes car il #
# comprend le code Ocaml comme une string #
# #
###########################################
*)
open Lexing
open SamenhirParser
open SamenhirAst
let code_buffer = Buffer.create 1024
}
let chiffre = ['0'-'9']
let alpha = ['a'-'z']|['A'-'Z']|'_'
let chaine = (alpha)(alpha | chiffre)*
let maj = ['A'-'Z']
let min = ['a'-'z']
let term = (maj)(alpha | chiffre)*
let notTerm = (min)(alpha | chiffre)*
let chaineCode = [^'%']
let chaineType = [^'>']
let space = " " | "\t"
rule token = parse
| "<" (chaineType* as c) ">" { TYPE c}
| "=" {ASSOC}
| "%{" (chaineCode* as c) "%}" { HEADER c}
| "{" {let _ = code lexbuf in let s = Buffer.contents code_buffer in Buffer.reset code_buffer; CODE s}
| notTerm as nt { N_TERM_IDENT nt}
| term as t {TERM t}
| ":" {POINTS}
| ";" {SEMICOLON}
| space {token lexbuf}
| "\n" {new_line lexbuf; token lexbuf}
| "%%" {SPLIT}
| "%token" {TOKEN}
| "%left" {LEFT}
| "%right" {RIGHT}
| "%start" {START}
| "%nonassoc" {NONASSOC}
| "%prec" {PREC}
| "|" {VERT}
| "/*" {comment lexbuf}
| eof {EOF}
| (_ as c) {print_char '\"';print_char c;print_char '\"';print_newline ();failwith "unknown char"}
and comment = parse
| "*/" {token lexbuf}
| _ {comment lexbuf}
and code = parse
| '}' { CODE ""}
| '{' {
Buffer.add_char code_buffer '{';
let _ = code lexbuf in
Buffer.add_char code_buffer '}';
code lexbuf
}
| '\n' {Buffer.add_char code_buffer '\n'; new_line lexbuf; code lexbuf}
| _ as c {Buffer.add_char code_buffer c; code lexbuf}