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
],
118 # b4_yysymprint_generate(FUNCTION-DECLARATOR)
119 # -------------------------------------------
120 # Generate the "symprint_" member function.
121 m4_define([b4_yysymprint_generate
],
122 [[/*--------------------------------.
123 | Print this symbol on YYOUTPUT. |
124 `--------------------------------*/
127 yy::b4_parser_class_name::symprint_ (int yytype
, const SemanticType
*yyvaluep
, const LocationType
*yylocationp
)[
129 /* Pacify ``unused variable'' warnings. */
133 cdebug_
<< (yytype
< ntokens_
? "token" : "nterm")
134 << ' ' << name_
[yytype
] << " (";
137 ]m4_map([b4_symbol_actions
], m4_defn([b4_symbol_printers
]))dnl
146 # b4_cxx_destruct_def(IGNORED-ARGUMENTS)
147 # --------------------------------------
148 # Declare the destruct_ method.
149 m4_define([b4_cxx_destruct_def
],
151 yy::b4_parser_class_name::destruct_ (int yytype
, SemanticType
*yyvaluep
, LocationType
*yylocationp
)[]dnl
155 # We do want M4 expansion after # for CPP macros.
158 m4_if(b4_defines_flag
, 0, [],
159 [@output @output_header_name@
160 b4_copyright([C
++ Skeleton parser
for LALR(1) parsing with Bison
],
162 /* FIXME: This is wrong, we want computed header guards.
163 I don't know why the macros are missing now. :( */
164 #ifndef PARSER_HEADER_H
165 # define PARSER_HEADER_H
168 #include "location.hh"
173 /* Using locations. */
174 #define YYLSP_NEEDED ]b4_locations_flag[
176 ]b4_token_defines(b4_tokens
)[
178 /* Copy the first part of user declarations. */
181 ]/* Line __line__ of lalr1.cc. */
182 b4_syncline([@oline@
], [@ofile@
])[
184 /* Enabling traces. */
186 # define YYDEBUG ]b4_debug[
189 /* Enabling verbose error message. */
190 #ifndef YYERROR_VERBOSE
191 # define YYERROR_VERBOSE ]b4_error_verbose[
195 typedef YYSTYPE yystype
;
197 ]m4_ifdef([b4_stype
],
198 [b4_syncline([b4_stype_line
], [b4_filename
])
199 typedef union b4_stype yystype
;
200 /* Line __line__ of lalr1.cc. */
201 b4_syncline([@oline@
], [@ofile@
])],
202 [typedef int yystype
;])[
203 # define YYSTYPE yystype
206 /* Copy the second part of user declarations. */
209 ]/* Line __line__ of lalr1.cc. */
210 b4_syncline([@oline@
], [@ofile@
])[
211 #ifndef YYLLOC_DEFAULT
212 # define YYLLOC_DEFAULT(Current, Rhs, N) \
213 ((Current).end = Rhs[N].end)
218 class ]b4_parser_class_name
[;
220 template < typename P
>
226 struct Traits
< ]b4_parser_class_name
[ >
228 typedef ]b4_int_type_for([b4_translate
])[ TokenNumberType
;
229 typedef ]b4_int_type_for([b4_rhs
])[ RhsNumberType
;
230 typedef int StateType
;
231 typedef yystype SemanticType
;
232 typedef ]b4_location_type
[ LocationType
;
238 class ]b4_parser_class_name b4_inherit
[
242 typedef Traits
< ]b4_parser_class_name
[ >::TokenNumberType TokenNumberType
;
243 typedef Traits
< ]b4_parser_class_name
[ >::RhsNumberType RhsNumberType
;
244 typedef Traits
< ]b4_parser_class_name
[ >::StateType StateType
;
245 typedef Traits
< ]b4_parser_class_name
[ >::SemanticType SemanticType
;
246 typedef Traits
< ]b4_parser_class_name
[ >::LocationType LocationType
;
248 typedef Stack
< StateType
> StateStack
;
249 typedef Stack
< SemanticType
> SemanticStack
;
250 typedef Stack
< LocationType
> LocationStack
;
253 ]b4_parser_class_name
[ (bool debug
,
254 LocationType initlocation
][]b4_param
[]b4_parse_param_decl
[) :
255 ]b4_constructor
[][debug_ (debug
),
257 initlocation_ (initlocation
)]b4_parse_param_cons
[
259 ]b4_parser_class_name
[ (bool debug
][]b4_param
[]b4_parse_param_decl
[) :
260 ]b4_constructor
[][debug_ (debug
),
261 cdebug_ (std::cerr
)]b4_parse_param_cons
[
266 virtual ~]b4_parser_class_name
[ ()
270 virtual int parse ();
274 virtual void lex_ ();
275 virtual void error_ ();
276 virtual void print_ ();
277 virtual void report_syntax_error_ ();
279 virtual void symprint_ (int yytype
,
280 const SemanticType
*yyvaluep
,
281 const LocationType
*yylocationp
);
282 #endif /* ! YYDEBUG */
286 StateStack state_stack_
;
287 SemanticStack semantic_stack_
;
288 LocationStack location_stack_
;
291 static const ]b4_int_type_for([b4_pact
])[ pact_
[];
292 static const ]b4_int_type(b4_pact_ninf
, b4_pact_ninf
)[ pact_ninf_
;
293 static const ]b4_int_type_for([b4_defact
])[ defact_
[];
294 static const ]b4_int_type_for([b4_pgoto
])[ pgoto_
[];
295 static const ]b4_int_type_for([b4_defgoto
])[ defgoto_
[];
296 static const ]b4_int_type_for([b4_table
])[ table_
[];
297 static const ]b4_int_type(b4_table_ninf
, b4_table_ninf
)[ table_ninf_
;
298 static const ]b4_int_type_for([b4_check
])[ check_
[];
299 static const ]b4_int_type_for([b4_stos
])[ stos_
[];
300 static const ]b4_int_type_for([b4_r1
])[ r1_
[];
301 static const ]b4_int_type_for([b4_r2
])[ r2_
[];
303 #if YYDEBUG || YYERROR_VERBOSE
304 static const char* const name_
[];
307 /* More tables, for debugging. */
309 static const RhsNumberType rhs_
[];
310 static const ]b4_int_type_for([b4_prhs
])[ prhs_
[];
311 static const ]b4_int_type_for([b4_rline
])[ rline_
[];
312 static const ]b4_int_type_for([b4_toknum
])[ token_number_
[];
313 virtual void reduce_print_ (int yyrule
);
314 virtual void stack_print_ ();
317 /* Even more tables. */
318 inline TokenNumberType
translate_ (int token
);
319 inline void destruct_ (int yytype
, SemanticType
*yyvaluep
,
320 LocationType
*yylocationp
);
323 static const int eof_
;
324 /* LAST_ -- Last index in TABLE_. */
325 static const int last_
;
326 static const int nnts_
;
327 static const int empty_
;
328 static const int final_
;
329 static const int terror_
;
330 static const int errcode_
;
331 static const int ntokens_
;
332 static const unsigned int user_token_number_max_
;
333 static const TokenNumberType undef_token_
;
340 /* Error handling. */
346 std::ostream
&cdebug_
;
348 /* Look-ahead and look-ahead in internal form. */
355 /* Semantic value and location of look-ahead token. */
357 LocationType location
;
358 /* Beginning of the last erroneous token popped off. */
359 Position error_start_
;
365 /* Initial location. */
366 LocationType initlocation_
;
367 ]b4_parse_param_vars
[
371 #endif /* ! defined PARSER_HEADER_H */]
373 @output @output_parser_name@
374 b4_copyright([C
++ Skeleton parser
for LALR(1) parsing with Bison
],
377 m4_if(b4_defines_flag
, 0, [], [#include @output_header_name@])[
379 /* Enable debugging if requested. */
386 # define YY_SYMBOL_PRINT(Title, Type, Value, Location) \
390 cdebug_ << (Title) << ' '; \
391 symprint_ ((Type), (Value), (Location)); \
392 cdebug_ << std::endl; \
396 # define YY_REDUCE_PRINT(Rule) \
399 reduce_print_ (Rule); \
402 # define YY_STACK_PRINT() \
410 # define YYCDEBUG if (0) cdebug_
411 # define YY_SYMBOL_PRINT(Title, Type, Value, Location)
412 # define YY_REDUCE_PRINT(Rule)
413 # define YY_STACK_PRINT()
415 #endif /* !YYDEBUG */
417 #define YYACCEPT goto yyacceptlab
418 #define YYABORT goto yyabortlab
419 #define YYERROR goto yyerrorlab
422 ]b4_yysymprint_generate([b4_cxx_symprint_def
])[
423 #endif /* ! YYDEBUG */
424 ]b4_yydestruct_generate([b4_cxx_destruct_def
])[
427 yy::]b4_parser_class_name
[::parse ()
432 /* Initialize the stacks. The initial state will be pushed in
433 yynewstate, since the latter expects the semantical and the
434 location values to have been already stored, initialize these
435 stacks with a primary value. */
436 state_stack_
= StateStack (0);
437 semantic_stack_
= SemanticStack (1);
438 location_stack_
= LocationStack (1);
444 location
= initlocation_
;
446 YYCDEBUG
<< "Starting parse" << std::endl
;
450 state_stack_
.push (state_
);
451 YYCDEBUG
<< "Entering state " << state_
<< std::endl
;
457 /* Try to take a decision without look-ahead. */
459 if (n_
== pact_ninf_
)
462 /* Read a look-ahead token. */
463 if (looka_
== empty_
)
465 YYCDEBUG
<< "Reading a token: ";
469 /* Convert token to internal form. */
472 looka_
= ilooka_
= eof_
;
473 YYCDEBUG
<< "Now at end of input." << std::endl
;
477 ilooka_
= translate_ (looka_
);
478 YY_SYMBOL_PRINT ("Next token is", ilooka_
, &value
, &location
);
481 /* If the proper action on seeing token ILOOKA_ is to reduce or to
482 detect an error, take that action. */
484 if (n_
< 0 || last_
< n_
|| check_
[n_
] != ilooka_
)
487 /* Reduce or error. */
491 if (n_
== table_ninf_
)
506 /* Shift the look-ahead token. */
508 YYCDEBUG
<< "Shifting token " << looka_
509 << " (" << name_
[ilooka_
] << "), ";
512 /* Discard the token being shifted unless it is eof. */
516 semantic_stack_
.push (value
);
517 location_stack_
.push (location
);
519 /* Count tokens shifted since error; after three, turn off error
527 /*-----------------------------------------------------------.
528 | yydefault -- do the default action for the current state. |
529 `-----------------------------------------------------------*/
531 n_
= defact_
[state_
];
536 /*-----------------------------.
537 | yyreduce -- Do a reduction. |
538 `-----------------------------*/
541 /* If LEN_ is nonzero, implement the default value of the action:
542 `$$ = $1'. Otherwise, use the top of the stack.
544 Otherwise, the following line sets YYVAL to garbage.
545 This behavior is undocumented and Bison
546 users should not rely upon it. */
549 yyval
= semantic_stack_
[len_
- 1];
550 yyloc
= location_stack_
[len_
- 1];
554 yyval
= semantic_stack_
[0];
555 yyloc
= location_stack_
[0];
560 Slice
< LocationType
, LocationStack
> slice (location_stack_
, len_
);
561 YYLLOC_DEFAULT (yyloc
, slice
, len_
);
563 YY_REDUCE_PRINT (n_
);
569 ]/* Line __line__ of lalr1.cc. */
570 b4_syncline([@oline@
], [@ofile@
])[
572 state_stack_
.pop (len_
);
573 semantic_stack_
.pop (len_
);
574 location_stack_
.pop (len_
);
578 semantic_stack_
.push (yyval
);
579 location_stack_
.push (yyloc
);
581 /* Shift the result of the reduction. */
583 state_
= pgoto_
[n_
- ntokens_
] + state_stack_
[0];
584 if (0 <= state_
&& state_
<= last_
&& check_
[state_
] == state_stack_
[0])
585 state_
= table_
[state_
];
587 state_
= defgoto_
[n_
- ntokens_
];
590 /*------------------------------------.
591 | yyerrlab -- here on detecting error |
592 `------------------------------------*/
594 /* If not already recovering from an error, report this error. */
595 report_syntax_error_ ();
597 error_start_
= location
.begin
;
600 /* If just tried and failed to reuse look-ahead token after an
601 error, discard it. */
603 /* Return failure if at end of input. */
606 /* If at end of input, pop the error token,
607 then the rest of the stack, then return failure. */
611 error_start_
= location_stack_
[0].begin
;
613 semantic_stack_
.pop ();
614 location_stack_
.pop ();
615 if (state_stack_
.height () == 1)
617 YY_SYMBOL_PRINT ("Error: popping",
618 stos_
[state_stack_
[0]],
620 &location_stack_
[0]);
621 destruct_ (stos_
[state_stack_
[0]],
623 &location_stack_
[0]);
628 YY_SYMBOL_PRINT ("Error: discarding", ilooka_
, &value
, &location
);
629 destruct_ (ilooka_
, &value
, &location
);
634 /* Else will try to reuse look-ahead token after shifting the error
639 /*---------------------------------------------------.
640 | yyerrorlab -- error raised explicitly by YYERROR. |
641 `---------------------------------------------------*/
645 /* Pacify GCC when the user code never invokes YYERROR and the label
646 yyerrorlab therefore never appears in user code. */
651 state_stack_
.pop (len_
);
652 semantic_stack_
.pop (len_
);
653 error_start_
= location_stack_
[len_
- 1].begin
;
654 location_stack_
.pop (len_
);
655 state_
= state_stack_
[0];
658 /*-------------------------------------------------------------.
659 | yyerrlab1 -- common code for both syntax error and YYERROR. |
660 `-------------------------------------------------------------*/
662 errstatus_
= 3; /* Each real token shifted decrements this. */
667 if (n_
!= pact_ninf_
)
670 if (0 <= n_
&& n_
<= last_
&& check_
[n_
] == terror_
)
678 /* Pop the current state because it cannot handle the error token. */
679 if (state_stack_
.height () == 1)
682 YY_SYMBOL_PRINT ("Error: popping",
684 &semantic_stack_
[0], &location_stack_
[0]);
685 destruct_ (stos_
[state_
], &semantic_stack_
[0], &location_stack_
[0]);
686 error_start_
= location_stack_
[0].begin
;
689 semantic_stack_
.pop ();
690 location_stack_
.pop ();
691 state_
= state_stack_
[0];
698 YYCDEBUG
<< "Shifting error token, ";
702 errloc
.begin
= error_start_
;
703 errloc
.end
= location
.end
;
704 semantic_stack_
.push (value
);
705 location_stack_
.push (errloc
);
721 yy::]b4_parser_class_name
[::lex_ ()
724 looka_
= yylex (&value
, &location
);
726 looka_
= yylex (&value
);
730 /** Generate an error message, and invoke yyerror. */
732 yy::]b4_parser_class_name
[::report_syntax_error_ ()
734 /* If not already recovering from an error, report this error. */
741 if (pact_ninf_
< n_
&& n_
< last_
)
743 message
= "syntax error, unexpected ";
744 message
+= name_
[ilooka_
];
747 for (int x
= (n_
< 0 ? -n_
: 0); x
< ntokens_
+ nnts_
; ++x
)
748 if (check_
[x
+ n_
] == x
&& x
!= terror_
)
753 for (int x
= (n_
< 0 ? -n_
: 0); x
< ntokens_
+ nnts_
; ++x
)
754 if (check_
[x
+ n_
] == x
&& x
!= terror_
)
756 message
+= (!count
++) ? ", expecting " : " or ";
764 message
= "syntax error";
770 /* YYPACT[STATE-NUM] -- Index in YYTABLE of the portion describing
772 const ]b4_int_type(b4_pact_ninf
, b4_pact_ninf
) yy::b4_parser_class_name::pact_ninf_
= b4_pact_ninf
[;
773 const ]b4_int_type_for([b4_pact
])[
774 yy::]b4_parser_class_name
[::pact_
[] =
779 /* YYDEFACT[S] -- default rule to reduce with in state S when YYTABLE
780 doesn't specify something else to do. Zero means the default is an
782 const ]b4_int_type_for([b4_defact
])[
783 yy::]b4_parser_class_name
[::defact_
[] =
788 /* YYPGOTO[NTERM-NUM]. */
789 const ]b4_int_type_for([b4_pgoto
])[
790 yy::]b4_parser_class_name
[::pgoto_
[] =
795 /* YYDEFGOTO[NTERM-NUM]. */
796 const ]b4_int_type_for([b4_defgoto
])[
797 yy::]b4_parser_class_name
[::defgoto_
[] =
802 /* YYTABLE[YYPACT[STATE-NUM]]. What to do in state STATE-NUM. If
803 positive, shift that token. If negative, reduce the rule which
804 number is the opposite. If zero, do what YYDEFACT says. */
805 const ]b4_int_type(b4_table_ninf
, b4_table_ninf
) yy::b4_parser_class_name::table_ninf_
= b4_table_ninf
[;
806 const ]b4_int_type_for([b4_table
])[
807 yy::]b4_parser_class_name
[::table_
[] =
813 const ]b4_int_type_for([b4_check
])[
814 yy::]b4_parser_class_name
[::check_
[] =
819 /* STOS_[STATE-NUM] -- The (internal number of the) accessing
820 symbol of state STATE-NUM. */
821 const ]b4_int_type_for([b4_stos
])[
822 yy::]b4_parser_class_name
[::stos_
[] =
828 /* TOKEN_NUMBER_[YYLEX-NUM] -- Internal token number corresponding
830 const ]b4_int_type_for([b4_toknum
])[
831 yy::]b4_parser_class_name
[::token_number_
[] =
837 /* YYR1[YYN] -- Symbol number of symbol that rule YYN derives. */
838 const ]b4_int_type_for([b4_r1
])[
839 yy::]b4_parser_class_name
[::r1_
[] =
844 /* YYR2[YYN] -- Number of symbols composing right hand side of rule YYN. */
845 const ]b4_int_type_for([b4_r2
])[
846 yy::]b4_parser_class_name
[::r2_
[] =
851 #if YYDEBUG || YYERROR_VERBOSE
852 /* YYTNAME[SYMBOL-NUM] -- String name of the symbol SYMBOL-NUM.
853 First, the terminals, then, starting at NTOKENS_, nonterminals. */
855 const yy::]b4_parser_class_name
[::name_
[] =
862 /* YYRHS -- A `-1'-separated list of the rules' RHS. */
863 const yy::]b4_parser_class_name
[::RhsNumberType
864 yy::]b4_parser_class_name
[::rhs_
[] =
869 /* YYPRHS[YYN] -- Index of the first RHS symbol of rule number YYN in
871 const ]b4_int_type_for([b4_prhs
])[
872 yy::]b4_parser_class_name
[::prhs_
[] =
877 /* YYRLINE[YYN] -- source line where rule number YYN was defined. */
878 const ]b4_int_type_for([b4_rline
])[
879 yy::]b4_parser_class_name
[::rline_
[] =
884 /** Print the state stack from its BOTTOM up to its TOP (included). */
887 yy::]b4_parser_class_name
[::stack_print_ ()
889 cdebug_
<< "state stack now";
890 for (StateStack::ConstIterator i
= state_stack_
.begin ();
891 i
!= state_stack_
.end (); ++i
)
892 cdebug_
<< ' ' << *i
;
893 cdebug_
<< std::endl
;
896 /** Report that the YYRULE is going to be reduced. */
899 yy::]b4_parser_class_name
[::reduce_print_ (int yyrule
)
901 unsigned int yylno
= rline_
[yyrule
];
902 /* Print the symbols being reduced, and their result. */
903 cdebug_
<< "Reducing via rule " << n_
- 1 << " (line " << yylno
<< "), ";
904 for (]b4_int_type_for([b4_prhs
])[ i
= prhs_
[n_
];
906 cdebug_
<< name_
[rhs_
[i
]] << ' ';
907 cdebug_
<< "-> " << name_
[r1_
[n_
]] << std::endl
;
911 /* YYTRANSLATE(YYLEX) -- Bison symbol number corresponding to YYLEX. */
912 yy::]b4_parser_class_name
[::TokenNumberType
913 yy::]b4_parser_class_name
[::translate_ (int token
)
916 const TokenNumberType
921 if ((unsigned int) token
<= user_token_number_max_
)
922 return translate_table
[token
];
927 const int yy::]b4_parser_class_name
[::eof_
= 0;
928 const int yy::]b4_parser_class_name
[::last_
= ]b4_last
[;
929 const int yy::]b4_parser_class_name
[::nnts_
= ]b4_nterms_number
[;
930 const int yy::]b4_parser_class_name
[::empty_
= -2;
931 const int yy::]b4_parser_class_name
[::final_
= ]b4_final_state_number
[;
932 const int yy::]b4_parser_class_name
[::terror_
= 1;
933 const int yy::]b4_parser_class_name
[::errcode_
= 256;
934 const int yy::]b4_parser_class_name
[::ntokens_
= ]b4_tokens_number
[;
936 const unsigned int yy::]b4_parser_class_name
[::user_token_number_max_
= ]b4_user_token_number_max
[;
937 const yy::]b4_parser_class_name
[::TokenNumberType
yy::]b4_parser_class_name
[::undef_token_
= ]b4_undef_token_number
[;
942 b4_copyright([Stack handling
for Bison C
++ parsers
], [2002, 2003, 2004])[
944 #ifndef BISON_STACK_HH
945 # define BISON_STACK_HH
951 template < class T
, class S
= std::deque
< T
> >
956 typedef typename
S::iterator Iterator
;
957 typedef typename
S::const_iterator ConstIterator
;
963 Stack (unsigned int n
) : seq_ (n
)
969 operator [] (unsigned int i
)
976 operator [] (unsigned int i
) const
990 pop (unsigned int n
= 1)
1000 return seq_
.size ();
1003 inline ConstIterator
begin () const { return seq_
.begin (); }
1004 inline ConstIterator
end () const { return seq_
.end (); }
1011 template < class T
, class S
= Stack
< T
> >
1016 Slice (const S
& stack
,
1017 unsigned int range
) : stack_ (stack
),
1024 operator [] (unsigned int i
) const
1026 return stack_
[range_
- i
];
1032 unsigned int range_
;
1036 #endif // not BISON_STACK_HH]
1039 b4_copyright([Position
class for Bison C
++ parsers
], [2002, 2003, 2004])[
1042 ** \file position.hh
1043 ** Define the Location class.
1046 #ifndef BISON_POSITION_HH
1047 # define BISON_POSITION_HH
1049 # include <iostream>
1054 /** \brief Abstract a Position. */
1058 /** \brief Initial column number. */
1059 static const unsigned int initial_column
= 0;
1060 /** \brief Initial line number. */
1061 static const unsigned int initial_line
= 1;
1063 /** \name Ctor & dtor.
1066 /** \brief Construct a Position. */
1069 line (initial_line
),
1070 column (initial_column
)
1076 /** \name Line and Column related manipulators
1079 /** \brief (line related) Advance to the COUNT next lines. */
1080 inline void lines (int count
= 1)
1082 column
= initial_column
;
1086 /** \brief (column related) Advance to the COUNT next columns. */
1087 inline void columns (int count
= 1)
1089 int leftmost
= initial_column
;
1090 int current
= column
;
1091 if (leftmost
<= current
+ count
)
1094 column
= initial_column
;
1099 /** \brief File name to which this position refers. */
1100 std::string filename
;
1101 /** \brief Current line number. */
1103 /** \brief Current column number. */
1104 unsigned int column
;
1107 /** \brief Add and assign a Position. */
1108 inline const Position
&
1109 operator+= (Position
& res
, const int width
)
1111 res
.columns (width
);
1115 /** \brief Add two Position objects. */
1116 inline const Position
1117 operator+ (const Position
& begin
, const int width
)
1119 Position res
= begin
;
1120 return res
+= width
;
1123 /** \brief Add and assign a Position. */
1124 inline const Position
&
1125 operator-= (Position
& res
, const int width
)
1127 return res
+= -width
;
1130 /** \brief Add two Position objects. */
1131 inline const Position
1132 operator- (const Position
& begin
, const int width
)
1134 return begin
+ -width
;
1137 /** \brief Intercept output stream redirection.
1138 ** \param ostr the destination output stream
1139 ** \param pos a reference to the Position to redirect
1141 inline std::ostream
&
1142 operator<< (std::ostream
& ostr
, const Position
& pos
)
1144 if (!pos
.filename
.empty ())
1145 ostr
<< pos
.filename
<< ':';
1146 return ostr
<< pos
.line
<< '.' << pos
.column
;
1150 #endif // not BISON_POSITION_HH]
1152 b4_copyright([Location
class for Bison C
++ parsers
], [2002, 2003, 2004])[
1155 ** \file location.hh
1156 ** Define the Location class.
1159 #ifndef BISON_LOCATION_HH
1160 # define BISON_LOCATION_HH
1162 # include <iostream>
1164 # include "position.hh"
1169 /** \brief Abstract a Location. */
1172 /** \name Ctor & dtor.
1175 /** \brief Construct a Location. */
1184 /** \name Line and Column related manipulators
1187 /** \brief Reset initial location to final location. */
1188 inline void step (void)
1193 /** \brief Extend the current location to the COUNT next columns. */
1194 inline void columns (unsigned int count
= 1)
1199 /** \brief Extend the current location to the COUNT next lines. */
1200 inline void lines (unsigned int count
= 1)
1208 /** \brief Beginning of the located region. */
1210 /** \brief End of the located region. */
1214 /** \brief Join two Location objects to create a Location. */
1215 inline const Location
operator+ (const Location
& begin
, const Location
& end
)
1217 Location res
= begin
;
1222 /** \brief Add two Location objects */
1223 inline const Location
operator+ (const Location
& begin
, unsigned int width
)
1225 Location res
= begin
;
1226 res
.columns (width
);
1230 /** \brief Add and assign a Location */
1231 inline Location
&operator+= (Location
& res
, unsigned int width
)
1233 res
.columns (width
);
1237 /** \brief Intercept output stream redirection.
1238 ** \param ostr the destination output stream
1239 ** \param loc a reference to the Location to redirect
1241 ** Avoid duplicate information.
1243 inline std::ostream
& operator<< (std::ostream
& ostr
, const Location
& loc
)
1245 Position last
= loc
.end
- 1;
1247 if (loc
.begin
.filename
!= last
.filename
)
1248 ostr
<< '-' << last
;
1249 else if (loc
.begin
.line
!= last
.line
)
1250 ostr
<< '-' << last
.line
<< '.' << last
.column
;
1251 else if (loc
.begin
.column
!= last
.column
)
1252 ostr
<< '-' << last
.column
;
1258 #endif // not BISON_LOCATION_HH]