aboutsummaryrefslogtreecommitdiff
path: root/src/odf/odf_ExpressionLexer.mll
blob: 00d1ce643a8a207beb3f311a5896f93920d0ee4c (plain)
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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
(*
This file is part of licht.

licht is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

licht is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with licht.  If not, see <http://www.gnu.org/licenses/>.
*)

{
  open Odf_ExpressionParser
  open Lexing

  exception SyntaxError of string
}

let digit = ['0'-'9']
let real = digit+ | digit* '.' digit+ | digit+ '.' digit*


let newline = "\r\n" | '\n' | '\r'
let space = ['\t' ' '] | newline

let letters = ['A'-'Z' 'a'-'z']

(* Function identifier.
   Valid identifiers are : 
     ORG.OPENOFFICE.DAYSINMONTH
   it cannot end with a digit.
 *)
let identifier = letters (letters | digit | ['-' '_' '.'])* letters+

let cell = letters+ digit+

rule read = parse
  | space+   { read lexbuf }

  | digit+ as _1 { NUM _1}
  | real as _1  { REAL (Tools.String.filter_float _1)}
  | '$'         { DOLLAR }

  | '='         { EQ }
  | "<>"        { NEQ }
  | '<'         { LT }
  | "<="        { LE }
  | '>'         { GT }
  | ">="        { GE }
  | '*'         { TIMES }
  | '+'         { PLUS }
  | '-'         { MINUS }
  | '/'         { DIVIDE }
  | '"'         { read_string (Buffer.create 16) lexbuf }
  | ';'         { SEMICOLON }
  | ':'         { COLON }
  | '['         { L_SQ_BRACKET }
  | ']'         { R_SQ_BRACKET }
  | '('         { LPAREN }
  | ')'         { RPAREN }
  | '^'         { POW }
  | '.'         { DOT }

  | letters+ as _1   { LETTERS _1}
  | identifier as _1 { IDENT _1}

  | '\000'      { EOF }
  | eof         { EOF }

and read_string buf = parse
  | '"'       { STR (Buffer.contents buf) }
  | '\\' '/'  { Buffer.add_char buf '/';    read_string buf lexbuf }
  | '\\' '\\' { Buffer.add_char buf '\\';   read_string buf lexbuf }
  | '\\' 'b'  { Buffer.add_char buf '\b';   read_string buf lexbuf }
  | '\\' 'f'  { Buffer.add_char buf '\012'; read_string buf lexbuf }
  | '\\' 'n'  { Buffer.add_char buf '\n';   read_string buf lexbuf }
  | '\\' 'r'  { Buffer.add_char buf '\r';   read_string buf lexbuf }
  | '\\' 't'  { Buffer.add_char buf '\t';   read_string buf lexbuf }
  | '\\' '"'  { Buffer.add_char buf '"';    read_string buf lexbuf }
  | [^ '"' '\\' '\000']+
    { Buffer.add_string buf (Lexing.lexeme lexbuf);
      read_string buf lexbuf
    }
  | '\000'  { STR (Buffer.contents buf) }
  | _ { raise (SyntaxError ("Illegal string character: " ^ Lexing.lexeme lexbuf)) }
  | eof { STR ( Buffer.contents buf) }

and quoteless_string buf = parse
  | '\\' '/'  { Buffer.add_char buf '/';    quoteless_string buf lexbuf }
  | '\\' '\\' { Buffer.add_char buf '\\';   quoteless_string buf lexbuf }
  | '\\' 'b'  { Buffer.add_char buf '\b';   quoteless_string buf lexbuf }
  | '\\' 'f'  { Buffer.add_char buf '\012'; quoteless_string buf lexbuf }
  | '\\' 'n'  { Buffer.add_char buf '\n';   quoteless_string buf lexbuf }
  | '\\' 'r'  { Buffer.add_char buf '\r';   quoteless_string buf lexbuf }
  | '\\' 't'  { Buffer.add_char buf '\t';   quoteless_string buf lexbuf }
  | '\\' '"'  { Buffer.add_char buf '"';    quoteless_string buf lexbuf }
  | [^ '\\' '\000']+
    { Buffer.add_string buf (Lexing.lexeme lexbuf);
      quoteless_string buf lexbuf
    }
  | '\000'  { STR (Buffer.contents buf) }
  | _ { raise (SyntaxError ("Illegal string character: " ^ Lexing.lexeme lexbuf)) }
  | eof { STR (Buffer.contents buf) }