1 (***********************************************************************)
5 (* Kim Nguyen, LRI UMR8623 *)
6 (* Université Paris-Sud & CNRS *)
8 (* Copyright 2010-2013 Université Paris-Sud and Centre National de la *)
9 (* Recherche Scientifique. All rights reserved. This file is *)
10 (* distributed under the terms of the GNU Lesser General Public *)
11 (* License, with the special exception on linking described in file *)
14 (***********************************************************************)
17 Time-stamp: <Last modified on 2013-02-05 14:53:12 CET by Kim Nguyen>
20 open Xpath_internal_parser
24 exception Error of int * int * string
26 let error i j s = raise (Error (i,j,s))
28 (***********************************************************)
29 (* Buffer for string literals *)
31 let string_buff = Buffer.create 1024
33 let store_lexeme lexbuf =
34 Buffer.add_string string_buff (Ulexing.utf8_lexeme lexbuf)
35 let store_ascii = Buffer.add_char string_buff
36 let store_code = Utf8.store string_buff
37 let clear_buff () = Buffer.clear string_buff
38 let get_stored_string () =
39 let s = Buffer.contents string_buff in
41 Buffer.clear string_buff;
44 (***********************************************************)
49 (L.lexeme_start lexbuf)
53 let return lexbuf tok = (tok, L.loc lexbuf)
54 let return_loc i j tok = (tok, (i,j))
56 let regexp ncname_char =
57 xml_letter | xml_digit | [ '-' '_' ] | xml_combining_char | xml_extender | "\\."
59 let hexa_digit = function
60 | '0'..'9' as c -> (Char.code c) - (Char.code '0')
61 | 'a'..'f' as c -> (Char.code c) - (Char.code 'a') + 10
62 | 'A'..'F' as c -> (Char.code c) - (Char.code 'A') + 10
65 let regexp ncname = ( xml_letter ncname_char* ) | ('_' ncname_char+)
66 let regexp digit = ['0'-'9']
67 let regexp float = '-'? digit+ ('.' digit+ (['e''E'] digit+)?)?
69 let parse_char lexbuf base i =
70 let s = L.latin1_sub_lexeme lexbuf i (L.lexeme_length lexbuf - i - 1) in
72 for i = 0 to String.length s - 1 do
73 let c = hexa_digit s.[i] in
74 if (c >= base) || (c < 0) then
75 error (L.lexeme_start lexbuf) (L.lexeme_end lexbuf) "invalid digit";
80 let keyword_or_tag s =
83 "self", AXIS Ast.Self;
84 "descendant", AXIS Ast.Descendant;
85 "child", AXIS Ast.Child;
86 "descendant-or-self", AXIS Ast.DescendantOrSelf;
87 "attribute", AXIS Ast.Attribute;
88 "following-sibling", AXIS Ast.FollowingSibling;
89 "preceding-sibling", AXIS Ast.PrecedingSibling;
90 "parent", AXIS Ast.Parent;
91 "ancestor", AXIS Ast.Ancestor;
92 "ancestor-or-self", AXIS Ast.AncestorOrSelf;
93 "preceding", AXIS Ast.Preceding;
94 "following", AXIS Ast.Following;
104 let rec token = lexer
105 | [' ' '\t' '\n'] -> token lexbuf
126 | ncname -> keyword_or_tag (L.utf8_lexeme lexbuf)
128 let s = L.utf8_lexeme lexbuf in
130 INT (int_of_string s)
132 _ -> FLOAT (float_of_string s))
134 let double_quote = L.latin1_lexeme_char lexbuf 0 = '"' in
135 string (L.lexeme_start lexbuf) double_quote lexbuf;
136 let s = get_stored_string () in
140 | _ -> illegal lexbuf
142 and string start double = lexer
144 let d = L.latin1_lexeme_char lexbuf 0 = '"' in
145 if d != double then (store_lexeme lexbuf; string start double lexbuf)
146 | '\\' ['\\' '"' '\''] ->
147 store_ascii (L.latin1_lexeme_char lexbuf 1);
148 string start double lexbuf
150 store_ascii '\n'; string start double lexbuf
152 store_ascii '\t'; string start double lexbuf
154 store_ascii '\r'; string start double lexbuf
155 | '\\' ['0'-'9']+ ';' ->
156 store_code (parse_char lexbuf 10 1);
157 string start double lexbuf
158 | '\\' 'x' ['0'-'9' 'a'-'f' 'A'-'F']+ ';' ->
159 store_code (parse_char lexbuf 16 2);
160 string start double lexbuf
164 error start (start+1) "Unterminated string"
167 string start double lexbuf