2 # C++ skeleton for Bison
3 # Copyright (C) 2002, 2003, 2004 Free Software Foundation, Inc.
5 # This program is free software; you can redistribute it and/or modify
6 # it under the terms of the GNU General Public License as published by
7 # the Free Software Foundation; either version 2 of the License, or
8 # (at your option) any later version.
10 # This program is distributed in the hope that it will be useful,
11 # but WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 # GNU General Public License for more details.
15 # You should have received a copy of the GNU General Public License
16 # along with this program; if not, write to the Free Software
17 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
20 ## ---------------- ##
22 ## ---------------- ##
24 # Default Parser class name.
25 m4_define_default([b4_parser_class_name
], [Parser
])
29 ## ----------------- ##
30 ## Semantic Values. ##
31 ## ----------------- ##
34 # b4_lhs_value([TYPE])
35 # --------------------
36 # Expansion of $<TYPE>$.
37 m4_define([b4_lhs_value
],
38 [yyval
[]m4_ifval([$
1], [.$
1])])
41 # b4_rhs_value(RULE-LENGTH, NUM, [TYPE])
42 # --------------------------------------
43 # Expansion of $<TYPE>NUM, where the current rule has RULE-LENGTH
45 m4_define([b4_rhs_value
],
46 [semantic_stack_@
{m4_eval([$
1 - $
2])@
}m4_ifval([$
3], [.$
3])])
48 m4_define_default([b4_location_type
], [Location
])
53 m4_define([b4_lhs_location
],
57 # b4_rhs_location(RULE-LENGTH, NUM)
58 # ---------------------------------
59 # Expansion of @NUM, where the current rule has RULE-LENGTH symbols
61 m4_define([b4_rhs_location
],
62 [location_stack_@
{m4_eval([$
1 - $
2])@
}])
65 m4_define([b4_inherit
],
77 m4_define([b4_constructor
],
86 # Constructor's extra arguments.
87 m4_define([b4_parse_param_decl],
88 [m4_ifset([b4_parse_param], [, b4_c_ansi_formals(b4_parse_param)])])
92 # constructor's extra initialisations.
93 m4_define([b4_parse_param_cons
],
94 [m4_ifset([b4_parse_param
],
96 b4_cc_constructor_calls(b4_parse_param
)])])
97 m4_define([b4_cc_constructor_calls
],
98 [m4_map_sep([b4_cc_constructor_call
], [,
100 m4_define([b4_cc_constructor_call
],
103 # b4_parse_param_vars
104 # -------------------
105 # Extra instance variables.
106 m4_define([b4_parse_param_vars
],
107 [m4_ifset([b4_parse_param
],
109 /* User arguments. */
110 b4_cc_var_decls(b4_parse_param
)])])
111 m4_define([b4_cc_var_decls
],
112 [m4_map_sep([b4_cc_var_decl
], [
114 m4_define([b4_cc_var_decl
],
119 # We do want M4 expansion after # for CPP macros.
122 m4_if(b4_defines_flag
, 0, [],
123 [@output @output_header_name@
124 b4_copyright([C
++ Skeleton parser
for LALR(1) parsing with Bison
],
126 /* FIXME: This is wrong, we want computed header guards.
127 I don't know why the macros are missing now. :( */
128 #ifndef PARSER_HEADER_H
129 # define PARSER_HEADER_H
132 #include "location.hh"
137 /* Using locations. */
138 #define YYLSP_NEEDED ]b4_locations_flag[
140 ]b4_token_defines(b4_tokens
)[
142 /* Copy the first part of user declarations. */
145 ]/* Line __line__ of lalr1.cc. */
146 b4_syncline([@oline@
], [@ofile@
])[
148 /* Enabling traces. */
150 # define YYDEBUG ]b4_debug[
153 /* Enabling verbose error message. */
154 #ifndef YYERROR_VERBOSE
155 # define YYERROR_VERBOSE ]b4_error_verbose[
159 typedef YYSTYPE yystype
;
161 ]m4_ifdef([b4_stype
],
162 [b4_syncline([b4_stype_line
], [b4_filename
])
163 typedef union b4_stype yystype
;
164 /* Line __line__ of lalr1.cc. */
165 b4_syncline([@oline@
], [@ofile@
])],
166 [typedef int yystype
;])[
167 # define YYSTYPE yystype
170 /* Copy the second part of user declarations. */
173 ]/* Line __line__ of lalr1.cc. */
174 b4_syncline([@oline@
], [@ofile@
])[
175 #ifndef YYLLOC_DEFAULT
176 # define YYLLOC_DEFAULT(Current, Rhs, N) \
177 ((Current).end = Rhs[N].end)
182 class ]b4_parser_class_name
[;
184 template < typename P
>
190 struct Traits
< ]b4_parser_class_name
[ >
192 typedef ]b4_int_type_for([b4_translate
])[ TokenNumberType
;
193 typedef ]b4_int_type_for([b4_rhs
])[ RhsNumberType
;
194 typedef int StateType
;
195 typedef yystype SemanticType
;
196 typedef ]b4_location_type
[ LocationType
;
202 class ]b4_parser_class_name b4_inherit
[
206 typedef Traits
< ]b4_parser_class_name
[ >::TokenNumberType TokenNumberType
;
207 typedef Traits
< ]b4_parser_class_name
[ >::RhsNumberType RhsNumberType
;
208 typedef Traits
< ]b4_parser_class_name
[ >::StateType StateType
;
209 typedef Traits
< ]b4_parser_class_name
[ >::SemanticType SemanticType
;
210 typedef Traits
< ]b4_parser_class_name
[ >::LocationType LocationType
;
212 typedef Stack
< StateType
> StateStack
;
213 typedef Stack
< SemanticType
> SemanticStack
;
214 typedef Stack
< LocationType
> LocationStack
;
217 ]b4_parser_class_name
[ (bool debug
,
218 LocationType initlocation
][]b4_param
[]b4_parse_param_decl
[) :
219 ]b4_constructor
[][debug_ (debug
),
221 initlocation_ (initlocation
)]b4_parse_param_cons
[
223 ]b4_parser_class_name
[ (bool debug
][]b4_param
[]b4_parse_param_decl
[) :
224 ]b4_constructor
[][debug_ (debug
),
225 cdebug_ (std::cerr
)]b4_parse_param_cons
[
230 virtual ~]b4_parser_class_name
[ ()
234 virtual int parse ();
238 virtual void lex_ ();
239 virtual void error_ ();
240 virtual void print_ ();
241 virtual void report_syntax_error_ ();
243 virtual void symprint_ (int yytype
,
244 const SemanticType
*yyvaluep
,
245 const LocationType
*yylocationp
);
246 #endif /* ! YYDEBUG */
250 StateStack state_stack_
;
251 SemanticStack semantic_stack_
;
252 LocationStack location_stack_
;
255 static const ]b4_int_type_for([b4_pact
])[ pact_
[];
256 static const ]b4_int_type(b4_pact_ninf
, b4_pact_ninf
)[ pact_ninf_
;
257 static const ]b4_int_type_for([b4_defact
])[ defact_
[];
258 static const ]b4_int_type_for([b4_pgoto
])[ pgoto_
[];
259 static const ]b4_int_type_for([b4_defgoto
])[ defgoto_
[];
260 static const ]b4_int_type_for([b4_table
])[ table_
[];
261 static const ]b4_int_type(b4_table_ninf
, b4_table_ninf
)[ table_ninf_
;
262 static const ]b4_int_type_for([b4_check
])[ check_
[];
263 static const ]b4_int_type_for([b4_stos
])[ stos_
[];
264 static const ]b4_int_type_for([b4_r1
])[ r1_
[];
265 static const ]b4_int_type_for([b4_r2
])[ r2_
[];
267 #if YYDEBUG || YYERROR_VERBOSE
268 static const char* const name_
[];
271 /* More tables, for debugging. */
273 static const RhsNumberType rhs_
[];
274 static const ]b4_int_type_for([b4_prhs
])[ prhs_
[];
275 static const ]b4_int_type_for([b4_rline
])[ rline_
[];
276 static const ]b4_int_type_for([b4_toknum
])[ token_number_
[];
277 virtual void reduce_print_ (int yyrule
);
278 virtual void stack_print_ ();
281 /* Even more tables. */
282 inline TokenNumberType
translate_ (int token
);
283 inline void destruct_ (const char *yymsg
,
285 SemanticType
*yyvaluep
, LocationType
*yylocationp
);
288 static const int eof_
;
289 /* LAST_ -- Last index in TABLE_. */
290 static const int last_
;
291 static const int nnts_
;
292 static const int empty_
;
293 static const int final_
;
294 static const int terror_
;
295 static const int errcode_
;
296 static const int ntokens_
;
297 static const unsigned int user_token_number_max_
;
298 static const TokenNumberType undef_token_
;
305 /* Error handling. */
311 std::ostream
&cdebug_
;
313 /* Look-ahead and look-ahead in internal form. */
320 /* Semantic value and location of look-ahead token. */
322 LocationType location
;
323 /* Beginning of the last erroneous token popped off. */
324 Position error_start_
;
330 /* Initial location. */
331 LocationType initlocation_
;
332 ]b4_parse_param_vars
[
336 #endif /* ! defined PARSER_HEADER_H */]
338 @output @output_parser_name@
339 b4_copyright([C
++ Skeleton parser
for LALR(1) parsing with Bison
],
342 m4_if(b4_defines_flag
, 0, [], [#include @output_header_name@])[
344 /* Enable debugging if requested. */
351 # define YY_SYMBOL_PRINT(Title, Type, Value, Location) \
355 cdebug_ << (Title) << ' '; \
356 symprint_ ((Type), (Value), (Location)); \
357 cdebug_ << std::endl; \
361 # define YY_REDUCE_PRINT(Rule) \
364 reduce_print_ (Rule); \
367 # define YY_STACK_PRINT() \
375 # define YYCDEBUG if (0) cdebug_
376 # define YY_SYMBOL_PRINT(Title, Type, Value, Location)
377 # define YY_REDUCE_PRINT(Rule)
378 # define YY_STACK_PRINT()
380 #endif /* !YYDEBUG */
382 #define YYACCEPT goto yyacceptlab
383 #define YYABORT goto yyabortlab
384 #define YYERROR goto yyerrorlab
387 /*--------------------------------.
388 | Print this symbol on YYOUTPUT. |
389 `--------------------------------*/
392 yy::]b4_parser_class_name
[::symprint_ (int yytype
,
393 const SemanticType
*yyvaluep
, const LocationType
*yylocationp
)
395 /* Pacify ``unused variable'' warnings. */
399 cdebug_
<< (yytype
< ntokens_
? "token" : "nterm")
400 << ' ' << name_
[yytype
] << " (";
403 ]m4_map([b4_symbol_actions
], m4_defn([b4_symbol_printers
]))dnl
409 #endif /* ! YYDEBUG */
412 yy::]b4_parser_class_name
[::destruct_ (const char *yymsg
,
413 int yytype
, SemanticType
*yyvaluep
, LocationType
*yylocationp
)
415 /* Pacify ``unused variable'' warnings. */
421 YY_SYMBOL_PRINT (yymsg
, yytype
, yyvaluep
, yylocationp
);
425 ]m4_map([b4_symbol_actions
], m4_defn([b4_symbol_destructors
]))[
432 yy::]b4_parser_class_name
[::parse ()
437 /* Initialize the stacks. The initial state will be pushed in
438 yynewstate, since the latter expects the semantical and the
439 location values to have been already stored, initialize these
440 stacks with a primary value. */
441 state_stack_
= StateStack (0);
442 semantic_stack_
= SemanticStack (1);
443 location_stack_
= LocationStack (1);
449 location
= initlocation_
;
451 YYCDEBUG
<< "Starting parse" << std::endl
;
455 state_stack_
.push (state_
);
456 YYCDEBUG
<< "Entering state " << state_
<< std::endl
;
462 /* Try to take a decision without look-ahead. */
464 if (n_
== pact_ninf_
)
467 /* Read a look-ahead token. */
468 if (looka_
== empty_
)
470 YYCDEBUG
<< "Reading a token: ";
474 /* Convert token to internal form. */
477 looka_
= ilooka_
= eof_
;
478 YYCDEBUG
<< "Now at end of input." << std::endl
;
482 ilooka_
= translate_ (looka_
);
483 YY_SYMBOL_PRINT ("Next token is", ilooka_
, &value
, &location
);
486 /* If the proper action on seeing token ILOOKA_ is to reduce or to
487 detect an error, take that action. */
489 if (n_
< 0 || last_
< n_
|| check_
[n_
] != ilooka_
)
492 /* Reduce or error. */
496 if (n_
== table_ninf_
)
511 /* Shift the look-ahead token. */
513 YYCDEBUG
<< "Shifting token " << looka_
514 << " (" << name_
[ilooka_
] << "), ";
517 /* Discard the token being shifted unless it is eof. */
521 semantic_stack_
.push (value
);
522 location_stack_
.push (location
);
524 /* Count tokens shifted since error; after three, turn off error
532 /*-----------------------------------------------------------.
533 | yydefault -- do the default action for the current state. |
534 `-----------------------------------------------------------*/
536 n_
= defact_
[state_
];
541 /*-----------------------------.
542 | yyreduce -- Do a reduction. |
543 `-----------------------------*/
546 /* If LEN_ is nonzero, implement the default value of the action:
547 `$$ = $1'. Otherwise, use the top of the stack.
549 Otherwise, the following line sets YYVAL to garbage.
550 This behavior is undocumented and Bison
551 users should not rely upon it. */
554 yyval
= semantic_stack_
[len_
- 1];
555 yyloc
= location_stack_
[len_
- 1];
559 yyval
= semantic_stack_
[0];
560 yyloc
= location_stack_
[0];
565 Slice
< LocationType
, LocationStack
> slice (location_stack_
, len_
);
566 YYLLOC_DEFAULT (yyloc
, slice
, len_
);
568 YY_REDUCE_PRINT (n_
);
574 ]/* Line __line__ of lalr1.cc. */
575 b4_syncline([@oline@
], [@ofile@
])[
577 state_stack_
.pop (len_
);
578 semantic_stack_
.pop (len_
);
579 location_stack_
.pop (len_
);
583 semantic_stack_
.push (yyval
);
584 location_stack_
.push (yyloc
);
586 /* Shift the result of the reduction. */
588 state_
= pgoto_
[n_
- ntokens_
] + state_stack_
[0];
589 if (0 <= state_
&& state_
<= last_
&& check_
[state_
] == state_stack_
[0])
590 state_
= table_
[state_
];
592 state_
= defgoto_
[n_
- ntokens_
];
595 /*------------------------------------.
596 | yyerrlab -- here on detecting error |
597 `------------------------------------*/
599 /* If not already recovering from an error, report this error. */
600 report_syntax_error_ ();
602 error_start_
= location
.begin
;
605 /* If just tried and failed to reuse look-ahead token after an
606 error, discard it. */
608 /* Return failure if at end of input. */
611 /* If at end of input, pop the error token,
612 then the rest of the stack, then return failure. */
616 error_start_
= location_stack_
[0].begin
;
618 semantic_stack_
.pop ();
619 location_stack_
.pop ();
620 if (state_stack_
.height () == 1)
622 destruct_ ("Error: popping",
623 stos_
[state_stack_
[0]],
625 &location_stack_
[0]);
630 destruct_ ("Error: discarding", ilooka_
, &value
, &location
);
635 /* Else will try to reuse look-ahead token after shifting the error
640 /*---------------------------------------------------.
641 | yyerrorlab -- error raised explicitly by YYERROR. |
642 `---------------------------------------------------*/
646 /* Pacify GCC when the user code never invokes YYERROR and the label
647 yyerrorlab therefore never appears in user code. */
652 state_stack_
.pop (len_
);
653 semantic_stack_
.pop (len_
);
654 error_start_
= location_stack_
[len_
- 1].begin
;
655 location_stack_
.pop (len_
);
656 state_
= state_stack_
[0];
659 /*-------------------------------------------------------------.
660 | yyerrlab1 -- common code for both syntax error and YYERROR. |
661 `-------------------------------------------------------------*/
663 errstatus_
= 3; /* Each real token shifted decrements this. */
668 if (n_
!= pact_ninf_
)
671 if (0 <= n_
&& n_
<= last_
&& check_
[n_
] == terror_
)
679 /* Pop the current state because it cannot handle the error token. */
680 if (state_stack_
.height () == 1)
683 destruct_ ("Error: popping",
684 stos_
[state_
], &semantic_stack_
[0], &location_stack_
[0]);
685 error_start_
= location_stack_
[0].begin
;
688 semantic_stack_
.pop ();
689 location_stack_
.pop ();
690 state_
= state_stack_
[0];
697 YYCDEBUG
<< "Shifting error token, ";
701 errloc
.begin
= error_start_
;
702 errloc
.end
= location
.end
;
703 semantic_stack_
.push (value
);
704 location_stack_
.push (errloc
);
716 /* Free the lookahead. */
717 destruct_ ("Error: discarding lookahead", ilooka_
, &value
, &location
);
723 yy::]b4_parser_class_name
[::lex_ ()
726 looka_
= yylex (&value
, &location
);
728 looka_
= yylex (&value
);
732 /** Generate an error message, and invoke yyerror. */
734 yy::]b4_parser_class_name
[::report_syntax_error_ ()
736 /* If not already recovering from an error, report this error. */
743 if (pact_ninf_
< n_
&& n_
< last_
)
745 message
= "syntax error, unexpected ";
746 message
+= name_
[ilooka_
];
749 for (int x
= (n_
< 0 ? -n_
: 0); x
< ntokens_
+ nnts_
; ++x
)
750 if (check_
[x
+ n_
] == x
&& x
!= terror_
)
755 for (int x
= (n_
< 0 ? -n_
: 0); x
< ntokens_
+ nnts_
; ++x
)
756 if (check_
[x
+ n_
] == x
&& x
!= terror_
)
758 message
+= (!count
++) ? ", expecting " : " or ";
766 message
= "syntax error";
772 /* YYPACT[STATE-NUM] -- Index in YYTABLE of the portion describing
774 const ]b4_int_type(b4_pact_ninf
, b4_pact_ninf
) yy::b4_parser_class_name::pact_ninf_
= b4_pact_ninf
[;
775 const ]b4_int_type_for([b4_pact
])[
776 yy::]b4_parser_class_name
[::pact_
[] =
781 /* YYDEFACT[S] -- default rule to reduce with in state S when YYTABLE
782 doesn't specify something else to do. Zero means the default is an
784 const ]b4_int_type_for([b4_defact
])[
785 yy::]b4_parser_class_name
[::defact_
[] =
790 /* YYPGOTO[NTERM-NUM]. */
791 const ]b4_int_type_for([b4_pgoto
])[
792 yy::]b4_parser_class_name
[::pgoto_
[] =
797 /* YYDEFGOTO[NTERM-NUM]. */
798 const ]b4_int_type_for([b4_defgoto
])[
799 yy::]b4_parser_class_name
[::defgoto_
[] =
804 /* YYTABLE[YYPACT[STATE-NUM]]. What to do in state STATE-NUM. If
805 positive, shift that token. If negative, reduce the rule which
806 number is the opposite. If zero, do what YYDEFACT says. */
807 const ]b4_int_type(b4_table_ninf
, b4_table_ninf
) yy::b4_parser_class_name::table_ninf_
= b4_table_ninf
[;
808 const ]b4_int_type_for([b4_table
])[
809 yy::]b4_parser_class_name
[::table_
[] =
815 const ]b4_int_type_for([b4_check
])[
816 yy::]b4_parser_class_name
[::check_
[] =
821 /* STOS_[STATE-NUM] -- The (internal number of the) accessing
822 symbol of state STATE-NUM. */
823 const ]b4_int_type_for([b4_stos
])[
824 yy::]b4_parser_class_name
[::stos_
[] =
830 /* TOKEN_NUMBER_[YYLEX-NUM] -- Internal token number corresponding
832 const ]b4_int_type_for([b4_toknum
])[
833 yy::]b4_parser_class_name
[::token_number_
[] =
839 /* YYR1[YYN] -- Symbol number of symbol that rule YYN derives. */
840 const ]b4_int_type_for([b4_r1
])[
841 yy::]b4_parser_class_name
[::r1_
[] =
846 /* YYR2[YYN] -- Number of symbols composing right hand side of rule YYN. */
847 const ]b4_int_type_for([b4_r2
])[
848 yy::]b4_parser_class_name
[::r2_
[] =
853 #if YYDEBUG || YYERROR_VERBOSE
854 /* YYTNAME[SYMBOL-NUM] -- String name of the symbol SYMBOL-NUM.
855 First, the terminals, then, starting at NTOKENS_, nonterminals. */
857 const yy::]b4_parser_class_name
[::name_
[] =
864 /* YYRHS -- A `-1'-separated list of the rules' RHS. */
865 const yy::]b4_parser_class_name
[::RhsNumberType
866 yy::]b4_parser_class_name
[::rhs_
[] =
871 /* YYPRHS[YYN] -- Index of the first RHS symbol of rule number YYN in
873 const ]b4_int_type_for([b4_prhs
])[
874 yy::]b4_parser_class_name
[::prhs_
[] =
879 /* YYRLINE[YYN] -- source line where rule number YYN was defined. */
880 const ]b4_int_type_for([b4_rline
])[
881 yy::]b4_parser_class_name
[::rline_
[] =
886 /** Print the state stack from its BOTTOM up to its TOP (included). */
889 yy::]b4_parser_class_name
[::stack_print_ ()
891 cdebug_
<< "state stack now";
892 for (StateStack::ConstIterator i
= state_stack_
.begin ();
893 i
!= state_stack_
.end (); ++i
)
894 cdebug_
<< ' ' << *i
;
895 cdebug_
<< std::endl
;
898 /** Report that the YYRULE is going to be reduced. */
901 yy::]b4_parser_class_name
[::reduce_print_ (int yyrule
)
903 unsigned int yylno
= rline_
[yyrule
];
904 /* Print the symbols being reduced, and their result. */
905 cdebug_
<< "Reducing via rule " << n_
- 1 << " (line " << yylno
<< "), ";
906 for (]b4_int_type_for([b4_prhs
])[ i
= prhs_
[n_
];
908 cdebug_
<< name_
[rhs_
[i
]] << ' ';
909 cdebug_
<< "-> " << name_
[r1_
[n_
]] << std::endl
;
913 /* YYTRANSLATE(YYLEX) -- Bison symbol number corresponding to YYLEX. */
914 yy::]b4_parser_class_name
[::TokenNumberType
915 yy::]b4_parser_class_name
[::translate_ (int token
)
918 const TokenNumberType
923 if ((unsigned int) token
<= user_token_number_max_
)
924 return translate_table
[token
];
929 const int yy::]b4_parser_class_name
[::eof_
= 0;
930 const int yy::]b4_parser_class_name
[::last_
= ]b4_last
[;
931 const int yy::]b4_parser_class_name
[::nnts_
= ]b4_nterms_number
[;
932 const int yy::]b4_parser_class_name
[::empty_
= -2;
933 const int yy::]b4_parser_class_name
[::final_
= ]b4_final_state_number
[;
934 const int yy::]b4_parser_class_name
[::terror_
= 1;
935 const int yy::]b4_parser_class_name
[::errcode_
= 256;
936 const int yy::]b4_parser_class_name
[::ntokens_
= ]b4_tokens_number
[;
938 const unsigned int yy::]b4_parser_class_name
[::user_token_number_max_
= ]b4_user_token_number_max
[;
939 const yy::]b4_parser_class_name
[::TokenNumberType
yy::]b4_parser_class_name
[::undef_token_
= ]b4_undef_token_number
[;
944 b4_copyright([Stack handling
for Bison C
++ parsers
], [2002, 2003, 2004])[
946 #ifndef BISON_STACK_HH
947 # define BISON_STACK_HH
953 template < class T
, class S
= std::deque
< T
> >
958 typedef typename
S::iterator Iterator
;
959 typedef typename
S::const_iterator ConstIterator
;
965 Stack (unsigned int n
) : seq_ (n
)
971 operator [] (unsigned int i
)
978 operator [] (unsigned int i
) const
992 pop (unsigned int n
= 1)
1002 return seq_
.size ();
1005 inline ConstIterator
begin () const { return seq_
.begin (); }
1006 inline ConstIterator
end () const { return seq_
.end (); }
1013 template < class T
, class S
= Stack
< T
> >
1018 Slice (const S
& stack
,
1019 unsigned int range
) : stack_ (stack
),
1026 operator [] (unsigned int i
) const
1028 return stack_
[range_
- i
];
1034 unsigned int range_
;
1038 #endif // not BISON_STACK_HH]
1041 b4_copyright([Position
class for Bison C
++ parsers
], [2002, 2003, 2004])[
1044 ** \file position.hh
1045 ** Define the Location class.
1048 #ifndef BISON_POSITION_HH
1049 # define BISON_POSITION_HH
1051 # include <iostream>
1056 /** \brief Abstract a Position. */
1060 /** \brief Initial column number. */
1061 static const unsigned int initial_column
= 0;
1062 /** \brief Initial line number. */
1063 static const unsigned int initial_line
= 1;
1065 /** \name Ctor & dtor.
1068 /** \brief Construct a Position. */
1071 line (initial_line
),
1072 column (initial_column
)
1078 /** \name Line and Column related manipulators
1081 /** \brief (line related) Advance to the COUNT next lines. */
1082 inline void lines (int count
= 1)
1084 column
= initial_column
;
1088 /** \brief (column related) Advance to the COUNT next columns. */
1089 inline void columns (int count
= 1)
1091 int leftmost
= initial_column
;
1092 int current
= column
;
1093 if (leftmost
<= current
+ count
)
1096 column
= initial_column
;
1101 /** \brief File name to which this position refers. */
1102 std::string filename
;
1103 /** \brief Current line number. */
1105 /** \brief Current column number. */
1106 unsigned int column
;
1109 /** \brief Add and assign a Position. */
1110 inline const Position
&
1111 operator+= (Position
& res
, const int width
)
1113 res
.columns (width
);
1117 /** \brief Add two Position objects. */
1118 inline const Position
1119 operator+ (const Position
& begin
, const int width
)
1121 Position res
= begin
;
1122 return res
+= width
;
1125 /** \brief Add and assign a Position. */
1126 inline const Position
&
1127 operator-= (Position
& res
, const int width
)
1129 return res
+= -width
;
1132 /** \brief Add two Position objects. */
1133 inline const Position
1134 operator- (const Position
& begin
, const int width
)
1136 return begin
+ -width
;
1139 /** \brief Intercept output stream redirection.
1140 ** \param ostr the destination output stream
1141 ** \param pos a reference to the Position to redirect
1143 inline std::ostream
&
1144 operator<< (std::ostream
& ostr
, const Position
& pos
)
1146 if (!pos
.filename
.empty ())
1147 ostr
<< pos
.filename
<< ':';
1148 return ostr
<< pos
.line
<< '.' << pos
.column
;
1152 #endif // not BISON_POSITION_HH]
1154 b4_copyright([Location
class for Bison C
++ parsers
], [2002, 2003, 2004])[
1157 ** \file location.hh
1158 ** Define the Location class.
1161 #ifndef BISON_LOCATION_HH
1162 # define BISON_LOCATION_HH
1164 # include <iostream>
1166 # include "position.hh"
1171 /** \brief Abstract a Location. */
1174 /** \name Ctor & dtor.
1177 /** \brief Construct a Location. */
1186 /** \name Line and Column related manipulators
1189 /** \brief Reset initial location to final location. */
1190 inline void step (void)
1195 /** \brief Extend the current location to the COUNT next columns. */
1196 inline void columns (unsigned int count
= 1)
1201 /** \brief Extend the current location to the COUNT next lines. */
1202 inline void lines (unsigned int count
= 1)
1210 /** \brief Beginning of the located region. */
1212 /** \brief End of the located region. */
1216 /** \brief Join two Location objects to create a Location. */
1217 inline const Location
operator+ (const Location
& begin
, const Location
& end
)
1219 Location res
= begin
;
1224 /** \brief Add two Location objects */
1225 inline const Location
operator+ (const Location
& begin
, unsigned int width
)
1227 Location res
= begin
;
1228 res
.columns (width
);
1232 /** \brief Add and assign a Location */
1233 inline Location
&operator+= (Location
& res
, unsigned int width
)
1235 res
.columns (width
);
1239 /** \brief Intercept output stream redirection.
1240 ** \param ostr the destination output stream
1241 ** \param loc a reference to the Location to redirect
1243 ** Avoid duplicate information.
1245 inline std::ostream
& operator<< (std::ostream
& ostr
, const Location
& loc
)
1247 Position last
= loc
.end
- 1;
1249 if (loc
.begin
.filename
!= last
.filename
)
1250 ostr
<< '-' << last
;
1251 else if (loc
.begin
.line
!= last
.line
)
1252 ostr
<< '-' << last
.line
<< '.' << last
.column
;
1253 else if (loc
.begin
.column
!= last
.column
)
1254 ostr
<< '-' << last
.column
;
1260 #endif // not BISON_LOCATION_HH]