@@ -67,6 +67,54 @@ let file_name (h : handle) = h.h_file_name
6767let linerec (h : handle ) = h.h_linerec
6868let curfile _ = (! current_handle).h_file_name
6969let curline _ = (! current_handle).h_lineno
70+ let has_gcc _ = (! current_handle).h_gcc
71+
72+
73+ (*
74+ * Error handling
75+ *)
76+ let underline_error (buffer : string ) (start : int ) (stop : int ) =
77+ let len = String. length buffer in
78+ let start' = max 0 start in
79+ let stop' = max 1 stop in
80+ (
81+ (if start' > 0 then (String. sub buffer 0 start') else " " )
82+ ^ " \027 [4m"
83+ ^ (if (stop' - start') <> 0
84+ then (String. sub buffer start' (stop' - start' ) )
85+ else " "
86+ )
87+ ^ " \027 [0m"
88+ ^ (if stop' < len then (String. sub buffer stop' (len - stop') ) else " " )
89+ )
90+
91+ let display_error msg token_start token_end =
92+ output_string (out_channel ! current_handle) (
93+ (if (interactive ! current_handle)
94+ then " "
95+ else
96+ (file_name ! current_handle) ^ " ["
97+ ^ (string_of_int (lineno ! current_handle)) ^ " ] "
98+ )
99+ ^ msg ^ " : "
100+ ^ (underline_error
101+ (line ! current_handle)
102+ (real_pos token_start ! current_handle)
103+ (real_pos token_end ! current_handle)
104+ )
105+ );
106+ flush (out_channel ! current_handle)
107+
108+ let display_semantic_error msg =
109+ display_error msg (pos ! current_handle) (pos ! current_handle)
110+
111+
112+ let error msg =
113+ display_error msg (Parsing. symbol_start () ) (Parsing. symbol_end () );
114+ raise Parsing. Parse_error
115+
116+ let test_gcc _ = if not (! current_handle).h_gcc then error " forbidden GCC syntax"
117+
70118
71119(*
72120** Keyword hashtable
@@ -81,10 +129,7 @@ struct
81129end
82130module StringHashtbl = Hashtbl. Make (HashString )
83131let lexicon = StringHashtbl. create 211
84- let init_lexicon _ =
85- StringHashtbl. clear lexicon;
86- List. iter
87- (fun (key , token ) -> StringHashtbl. add lexicon key token)
132+ let keywords =
88133 [
89134 (" auto" , id AUTO );
90135 (" const" , id CONST ); (" __const" , id CONST );
@@ -120,11 +165,20 @@ let init_lexicon _ =
120165 (" if" , fun _ -> IF (curfile() , curline() ));
121166 (" else" , fun _ -> ELSE (curfile() , curline() ));
122167 (" asm" , id ASM );
123-
124- (* ** Specific GNU ***)
168+ ]
169+
170+ (* ** Specific GNU ***)
171+ let gnu_keywords : (string * (unit -> Cparser.token)) list = [
125172 (" __attribute__" , id ATTRIBUTE );
126- (" __extension__" , id EXTENSION )
127- ]
173+ (" __extension__" , id EXTENSION );
174+ (" __inline" , id INLINE )
175+ ]
176+
177+ let init_lexicon _ =
178+ let add (key , token ) = StringHashtbl. add lexicon key token in
179+ StringHashtbl. clear lexicon;
180+ List. iter add keywords;
181+ if has_gcc () then List. iter add gnu_keywords
128182
129183let add_type name =
130184 StringHashtbl. add lexicon name (id (NAMED_TYPE name))
@@ -167,51 +221,6 @@ let set_name name =
167221 (! current_handle).h_file_name < - name
168222
169223
170- (* ** syntax error building ***)
171- let underline_error (buffer : string ) (start : int ) (stop : int ) =
172- let len = String. length buffer in
173- let start' = max 0 start in
174- let stop' = max 1 stop in
175- (
176- (if start' > 0 then (String. sub buffer 0 start') else " " )
177- ^ " \027 [4m"
178- ^ (if (stop' - start') <> 0
179- then (String. sub buffer start' (stop' - start' ) )
180- else " "
181- )
182- ^ " \027 [0m"
183- ^ (if stop' < len then (String. sub buffer stop' (len - stop') ) else " " )
184- )
185-
186- let display_error msg token_start token_end =
187- output_string (out_channel ! current_handle) (
188- (if (interactive ! current_handle)
189- then " "
190- else
191- (file_name ! current_handle) ^ " ["
192- ^ (string_of_int (lineno ! current_handle)) ^ " ] "
193- )
194- ^ msg ^ " : "
195- ^ (underline_error
196- (line ! current_handle)
197- (real_pos token_start ! current_handle)
198- (real_pos token_end ! current_handle)
199- )
200- );
201- flush (out_channel ! current_handle)
202-
203- let display_semantic_error msg =
204- display_error msg (pos ! current_handle) (pos ! current_handle)
205-
206-
207- (* ** Error handling ***)
208- let error msg =
209- display_error msg (Parsing. symbol_start () ) (Parsing. symbol_end () );
210- raise Parsing. Parse_error
211-
212- let test_gcc _ = if not (! current_handle).h_gcc then error " forbidden GCC syntax"
213-
214-
215224(* ** escape character management ***)
216225let scan_escape str =
217226 match str with
0 commit comments