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 (***********************************************************************)
16 open Xpath_internal_parser
20 exception Error of int * int * string
22 let error i j s = raise (Error (i,j,s))
24 (***********************************************************)
25 (* Buffer for string literals *)
27 let string_buff = Buffer.create 1024
29 let store_lexeme lexbuf =
30 Buffer.add_string string_buff (Ulexing.utf8_lexeme lexbuf)
31 let store_ascii = Buffer.add_char string_buff
32 let store_code = Utf8.store string_buff
33 let clear_buff () = Buffer.clear string_buff
34 let get_stored_string () =
35 let s = Buffer.contents string_buff in
37 Buffer.clear string_buff;
40 (***********************************************************)
45 (L.lexeme_start lexbuf)
49 let return lexbuf tok = (tok, L.loc lexbuf)
50 let return_loc i j tok = (tok, (i,j))
52 let regexp ncname_char =
53 xml_letter | xml_digit | [ '-' '_' '.'] | xml_combining_char | xml_extender
55 let hexa_digit = function
56 | '0'..'9' as c -> (Char.code c) - (Char.code '0')
57 | 'a'..'f' as c -> (Char.code c) - (Char.code 'a') + 10
58 | 'A'..'F' as c -> (Char.code c) - (Char.code 'A') + 10
61 let regexp ncname = ( xml_letter ncname_char* ) | ('_' ncname_char+)
62 let regexp digit = ['0'-'9']
63 let regexp float = '-'? digit+ ('.' digit+ (['e''E'] digit+)?)?
65 let parse_char lexbuf base i =
66 let s = L.latin1_sub_lexeme lexbuf i (L.lexeme_length lexbuf - i - 1) in
68 for i = 0 to String.length s - 1 do
69 let c = hexa_digit s.[i] in
70 if (c >= base) || (c < 0) then
71 error (L.lexeme_start lexbuf) (L.lexeme_end lexbuf) "invalid digit";
76 let keyword_or_tag s =
79 "self", AXIS Ast.Self;
80 "descendant", AXIS (Ast.Descendant false);
81 "child", AXIS Ast.Child;
82 "descendant-or-self", AXIS (Ast.Descendant true);
83 "attribute", AXIS Ast.Attribute;
84 "following-sibling", AXIS Ast.FollowingSibling;
85 "preceding-sibling", AXIS Ast.PrecedingSibling;
86 "parent", AXIS Ast.Parent;
87 "ancestor", AXIS (Ast.Ancestor false);
88 "ancestor-or-self", AXIS (Ast.Ancestor true);
89 "preceding", AXIS Ast.Preceding;
90 "following", AXIS Ast.Following;
100 let rec token = lexer
101 | [' ' '\t' '\n'] -> token lexbuf
124 | "comment()" -> COMMENT
125 | '@' ncname -> ATTNAME (L.utf8_lexeme lexbuf)
126 | "processing-instruction()" -> PI ""
127 | "processing-instruction('"ncname"')"
128 | "processing-instruction(\""ncname"\")"->
129 let s = L.utf8_lexeme lexbuf in
130 PI (String.sub s 24 (String.length s - 26))
131 | ncname -> keyword_or_tag (L.utf8_lexeme lexbuf)
133 let s = L.utf8_lexeme lexbuf in
135 INT (int_of_string s)
137 _ -> FLOAT (float_of_string s))
139 let double_quote = L.latin1_lexeme_char lexbuf 0 = '"' in
140 string (L.lexeme_start lexbuf) double_quote lexbuf;
141 let s = get_stored_string () in
145 | _ -> illegal lexbuf
147 and string start double = lexer
149 let d = L.latin1_lexeme_char lexbuf 0 = '"' in
150 if d != double then (store_lexeme lexbuf; string start double lexbuf)
151 | '\\' ['\\' '"' '\''] ->
152 store_ascii (L.latin1_lexeme_char lexbuf 1);
153 string start double lexbuf
155 store_ascii '\n'; string start double lexbuf
157 store_ascii '\t'; string start double lexbuf
159 store_ascii '\r'; string start double lexbuf
160 | '\\' ['0'-'9']+ ';' ->
161 store_code (parse_char lexbuf 10 1);
162 string start double lexbuf
163 | '\\' 'x' ['0'-'9' 'a'-'f' 'A'-'F']+ ';' ->
164 store_code (parse_char lexbuf 16 2);
165 string start double lexbuf
169 error start (start+1) "Unterminated string"
172 string start double lexbuf