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 /* YYLLOC_DEFAULT -- Set CURRENT to span from RHS[1] to RHS[N]. */
177 #ifndef YYLLOC_DEFAULT
178 # define YYLLOC_DEFAULT(Current, Rhs, N) \
180 ((Current).begin = (Rhs)[1].begin); \
181 ((Current).end = (Rhs)[N].end); \
187 class ]b4_parser_class_name
[;
189 template < typename P
>
195 struct Traits
< ]b4_parser_class_name
[ >
197 typedef ]b4_int_type_for([b4_translate
])[ TokenNumberType
;
198 typedef ]b4_int_type_for([b4_rhs
])[ RhsNumberType
;
199 typedef int StateType
;
200 typedef yystype SemanticType
;
201 typedef ]b4_location_type
[ LocationType
;
207 class ]b4_parser_class_name b4_inherit
[
211 typedef Traits
< ]b4_parser_class_name
[ >::TokenNumberType TokenNumberType
;
212 typedef Traits
< ]b4_parser_class_name
[ >::RhsNumberType RhsNumberType
;
213 typedef Traits
< ]b4_parser_class_name
[ >::StateType StateType
;
214 typedef Traits
< ]b4_parser_class_name
[ >::SemanticType SemanticType
;
215 typedef Traits
< ]b4_parser_class_name
[ >::LocationType LocationType
;
217 typedef Stack
< StateType
> StateStack
;
218 typedef Stack
< SemanticType
> SemanticStack
;
219 typedef Stack
< LocationType
> LocationStack
;
222 ]b4_parser_class_name
[ (bool debug
,
223 LocationType initlocation
][]b4_param
[]b4_parse_param_decl
[) :
224 ]b4_constructor
[][debug_ (debug
),
226 initlocation_ (initlocation
)]b4_parse_param_cons
[
228 ]b4_parser_class_name
[ (bool debug
][]b4_param
[]b4_parse_param_decl
[) :
229 ]b4_constructor
[][debug_ (debug
),
230 cdebug_ (std::cerr
)]b4_parse_param_cons
[
235 virtual ~]b4_parser_class_name
[ ()
239 virtual int parse ();
243 virtual void lex_ ();
244 virtual void error_ ();
245 virtual void print_ ();
246 virtual void report_syntax_error_ ();
248 virtual void symprint_ (int yytype
,
249 const SemanticType
*yyvaluep
,
250 const LocationType
*yylocationp
);
251 #endif /* ! YYDEBUG */
255 StateStack state_stack_
;
256 SemanticStack semantic_stack_
;
257 LocationStack location_stack_
;
260 static const ]b4_int_type_for([b4_pact
])[ pact_
[];
261 static const ]b4_int_type(b4_pact_ninf
, b4_pact_ninf
)[ pact_ninf_
;
262 static const ]b4_int_type_for([b4_defact
])[ defact_
[];
263 static const ]b4_int_type_for([b4_pgoto
])[ pgoto_
[];
264 static const ]b4_int_type_for([b4_defgoto
])[ defgoto_
[];
265 static const ]b4_int_type_for([b4_table
])[ table_
[];
266 static const ]b4_int_type(b4_table_ninf
, b4_table_ninf
)[ table_ninf_
;
267 static const ]b4_int_type_for([b4_check
])[ check_
[];
268 static const ]b4_int_type_for([b4_stos
])[ stos_
[];
269 static const ]b4_int_type_for([b4_r1
])[ r1_
[];
270 static const ]b4_int_type_for([b4_r2
])[ r2_
[];
272 #if YYDEBUG || YYERROR_VERBOSE
273 static const char* const name_
[];
276 /* More tables, for debugging. */
278 static const RhsNumberType rhs_
[];
279 static const ]b4_int_type_for([b4_prhs
])[ prhs_
[];
280 static const ]b4_int_type_for([b4_rline
])[ rline_
[];
281 static const ]b4_int_type_for([b4_toknum
])[ token_number_
[];
282 virtual void reduce_print_ (int yyrule
);
283 virtual void stack_print_ ();
286 /* Even more tables. */
287 inline TokenNumberType
translate_ (int token
);
288 inline void destruct_ (const char *yymsg
,
290 SemanticType
*yyvaluep
, LocationType
*yylocationp
);
292 /// Pop \a n symbols the three stacks.
293 inline void pop (unsigned int n
= 1);
296 static const int eof_
;
297 /* LAST_ -- Last index in TABLE_. */
298 static const int last_
;
299 static const int nnts_
;
300 static const int empty_
;
301 static const int final_
;
302 static const int terror_
;
303 static const int errcode_
;
304 static const int ntokens_
;
305 static const unsigned int user_token_number_max_
;
306 static const TokenNumberType undef_token_
;
313 /* Error handling. */
319 std::ostream
&cdebug_
;
321 /* Look-ahead and look-ahead in internal form. */
328 /* Semantic value and location of look-ahead token. */
330 LocationType location
;
331 /// The locations where the error started and ended.
332 Location error_range_
[2];
338 /* Initial location. */
339 LocationType initlocation_
;
340 ]b4_parse_param_vars
[
344 #endif /* ! defined PARSER_HEADER_H */]
346 @output @output_parser_name@
347 b4_copyright([C
++ Skeleton parser
for LALR(1) parsing with Bison
],
350 m4_if(b4_defines_flag
, 0, [], [#include @output_header_name@])[
352 /* Enable debugging if requested. */
359 # define YY_SYMBOL_PRINT(Title, Type, Value, Location) \
363 cdebug_ << (Title) << ' '; \
364 symprint_ ((Type), (Value), (Location)); \
365 cdebug_ << std::endl; \
369 # define YY_REDUCE_PRINT(Rule) \
372 reduce_print_ (Rule); \
375 # define YY_STACK_PRINT() \
383 # define YYCDEBUG if (0) cdebug_
384 # define YY_SYMBOL_PRINT(Title, Type, Value, Location)
385 # define YY_REDUCE_PRINT(Rule)
386 # define YY_STACK_PRINT()
388 #endif /* !YYDEBUG */
390 #define YYACCEPT goto yyacceptlab
391 #define YYABORT goto yyabortlab
392 #define YYERROR goto yyerrorlab
395 /*--------------------------------.
396 | Print this symbol on YYOUTPUT. |
397 `--------------------------------*/
400 yy::]b4_parser_class_name
[::symprint_ (int yytype
,
401 const SemanticType
*yyvaluep
, const LocationType
*yylocationp
)
403 /* Pacify ``unused variable'' warnings. */
407 cdebug_
<< (yytype
< ntokens_
? "token" : "nterm")
408 << ' ' << name_
[yytype
] << " ("
409 << *yylocationp
<< ": ";
412 ]m4_map([b4_symbol_actions
], m4_defn([b4_symbol_printers
]))dnl
418 #endif /* ! YYDEBUG */
421 yy::]b4_parser_class_name
[::destruct_ (const char *yymsg
,
422 int yytype
, SemanticType
*yyvaluep
, LocationType
*yylocationp
)
424 /* Pacify ``unused variable'' warnings. */
428 YY_SYMBOL_PRINT (yymsg
, yytype
, yyvaluep
, yylocationp
);
432 ]m4_map([b4_symbol_actions
], m4_defn([b4_symbol_destructors
]))[
439 yy::]b4_parser_class_name
[::pop (unsigned int n
)
441 state_stack_
.pop (n
);
442 semantic_stack_
.pop (n
);
443 location_stack_
.pop (n
);
447 yy::]b4_parser_class_name
[::parse ()
452 /* Initialize the stacks. The initial state will be pushed in
453 yynewstate, since the latter expects the semantical and the
454 location values to have been already stored, initialize these
455 stacks with a primary value. */
456 state_stack_
= StateStack (0);
457 semantic_stack_
= SemanticStack (1);
458 location_stack_
= LocationStack (1);
464 location
= initlocation_
;
466 YYCDEBUG
<< "Starting parse" << std::endl
;
470 state_stack_
.push (state_
);
471 YYCDEBUG
<< "Entering state " << state_
<< std::endl
;
477 /* Try to take a decision without look-ahead. */
479 if (n_
== pact_ninf_
)
482 /* Read a look-ahead token. */
483 if (looka_
== empty_
)
485 YYCDEBUG
<< "Reading a token: ";
489 /* Convert token to internal form. */
492 looka_
= ilooka_
= eof_
;
493 YYCDEBUG
<< "Now at end of input." << std::endl
;
497 ilooka_
= translate_ (looka_
);
498 YY_SYMBOL_PRINT ("Next token is", ilooka_
, &value
, &location
);
501 /* If the proper action on seeing token ILOOKA_ is to reduce or to
502 detect an error, take that action. */
504 if (n_
< 0 || last_
< n_
|| check_
[n_
] != ilooka_
)
507 /* Reduce or error. */
511 if (n_
== table_ninf_
)
526 /* Shift the look-ahead token. */
527 YY_SYMBOL_PRINT ("Shifting", ilooka_
, &value
, &location
);
529 /* Discard the token being shifted unless it is eof. */
533 semantic_stack_
.push (value
);
534 location_stack_
.push (location
);
536 /* Count tokens shifted since error; after three, turn off error
544 /*-----------------------------------------------------------.
545 | yydefault -- do the default action for the current state. |
546 `-----------------------------------------------------------*/
548 n_
= defact_
[state_
];
553 /*-----------------------------.
554 | yyreduce -- Do a reduction. |
555 `-----------------------------*/
558 /* If LEN_ is nonzero, implement the default value of the action:
559 `$$ = $1'. Otherwise, use the top of the stack.
561 Otherwise, the following line sets YYVAL to garbage.
562 This behavior is undocumented and Bison
563 users should not rely upon it. */
566 yyval
= semantic_stack_
[len_
- 1];
567 yyloc
= location_stack_
[len_
- 1];
571 yyval
= semantic_stack_
[0];
572 yyloc
= location_stack_
[0];
577 Slice
< LocationType
, LocationStack
> slice (location_stack_
, len_
);
578 YYLLOC_DEFAULT (yyloc
, slice
, len_
);
580 YY_REDUCE_PRINT (n_
);
586 ]/* Line __line__ of lalr1.cc. */
587 b4_syncline([@oline@
], [@ofile@
])[
593 semantic_stack_
.push (yyval
);
594 location_stack_
.push (yyloc
);
596 /* Shift the result of the reduction. */
598 state_
= pgoto_
[n_
- ntokens_
] + state_stack_
[0];
599 if (0 <= state_
&& state_
<= last_
&& check_
[state_
] == state_stack_
[0])
600 state_
= table_
[state_
];
602 state_
= defgoto_
[n_
- ntokens_
];
605 /*------------------------------------.
606 | yyerrlab -- here on detecting error |
607 `------------------------------------*/
609 /* If not already recovering from an error, report this error. */
610 report_syntax_error_ ();
612 error_range_
[0] = location
;
615 /* If just tried and failed to reuse look-ahead token after an
616 error, discard it. */
618 /* Return failure if at end of input. */
621 /* If at end of input, pop the error token,
622 then the rest of the stack, then return failure. */
626 error_range_
[0] = location_stack_
[0];
628 if (state_stack_
.height () == 1)
630 destruct_ ("Error: popping",
631 stos_
[state_stack_
[0]],
633 &location_stack_
[0]);
638 destruct_ ("Error: discarding", ilooka_
, &value
, &location
);
643 /* Else will try to reuse look-ahead token after shifting the error
648 /*---------------------------------------------------.
649 | yyerrorlab -- error raised explicitly by YYERROR. |
650 `---------------------------------------------------*/
654 /* Pacify GCC when the user code never invokes YYERROR and the label
655 yyerrorlab therefore never appears in user code. */
660 error_range_
[0] = location_stack_
[len_
- 1];
662 state_
= state_stack_
[0];
665 /*-------------------------------------------------------------.
666 | yyerrlab1 -- common code for both syntax error and YYERROR. |
667 `-------------------------------------------------------------*/
669 errstatus_
= 3; /* Each real token shifted decrements this. */
674 if (n_
!= pact_ninf_
)
677 if (0 <= n_
&& n_
<= last_
&& check_
[n_
] == terror_
)
685 /* Pop the current state because it cannot handle the error token. */
686 if (state_stack_
.height () == 1)
689 error_range_
[0] = location_stack_
[0];
690 destruct_ ("Error: popping",
691 stos_
[state_
], &semantic_stack_
[0], &location_stack_
[0]);
693 state_
= state_stack_
[0];
700 error_range_
[1] = location
;
701 // Using LOCATION is tempting, but would change the location of
702 // the look-ahead. YYLOC is available though.
703 YYLLOC_DEFAULT (yyloc
, error_range_
- 1, 2);
704 semantic_stack_
.push (value
);
705 location_stack_
.push (yyloc
);
707 /* Shift the error token. */
708 YY_SYMBOL_PRINT ("Shifting", stos_
[n_
],
709 &semantic_stack_
[0], &location_stack_
[0]);
720 /* Free the lookahead. */
721 destruct_ ("Error: discarding lookahead", ilooka_
, &value
, &location
);
727 yy::]b4_parser_class_name
[::lex_ ()
730 looka_
= yylex (&value
, &location
);
732 looka_
= yylex (&value
);
736 /** Generate an error message, and invoke yyerror. */
738 yy::]b4_parser_class_name
[::report_syntax_error_ ()
740 /* If not already recovering from an error, report this error. */
747 if (pact_ninf_
< n_
&& n_
< last_
)
749 message
= "syntax error, unexpected ";
750 message
+= name_
[ilooka_
];
753 for (int x
= (n_
< 0 ? -n_
: 0); x
< ntokens_
+ nnts_
; ++x
)
754 if (check_
[x
+ n_
] == x
&& x
!= terror_
)
759 for (int x
= (n_
< 0 ? -n_
: 0); x
< ntokens_
+ nnts_
; ++x
)
760 if (check_
[x
+ n_
] == x
&& x
!= terror_
)
762 message
+= (!count
++) ? ", expecting " : " or ";
770 message
= "syntax error";
776 /* YYPACT[STATE-NUM] -- Index in YYTABLE of the portion describing
778 const ]b4_int_type(b4_pact_ninf
, b4_pact_ninf
) yy::b4_parser_class_name::pact_ninf_
= b4_pact_ninf
[;
779 const ]b4_int_type_for([b4_pact
])[
780 yy::]b4_parser_class_name
[::pact_
[] =
785 /* YYDEFACT[S] -- default rule to reduce with in state S when YYTABLE
786 doesn't specify something else to do. Zero means the default is an
788 const ]b4_int_type_for([b4_defact
])[
789 yy::]b4_parser_class_name
[::defact_
[] =
794 /* YYPGOTO[NTERM-NUM]. */
795 const ]b4_int_type_for([b4_pgoto
])[
796 yy::]b4_parser_class_name
[::pgoto_
[] =
801 /* YYDEFGOTO[NTERM-NUM]. */
802 const ]b4_int_type_for([b4_defgoto
])[
803 yy::]b4_parser_class_name
[::defgoto_
[] =
808 /* YYTABLE[YYPACT[STATE-NUM]]. What to do in state STATE-NUM. If
809 positive, shift that token. If negative, reduce the rule which
810 number is the opposite. If zero, do what YYDEFACT says. */
811 const ]b4_int_type(b4_table_ninf
, b4_table_ninf
) yy::b4_parser_class_name::table_ninf_
= b4_table_ninf
[;
812 const ]b4_int_type_for([b4_table
])[
813 yy::]b4_parser_class_name
[::table_
[] =
819 const ]b4_int_type_for([b4_check
])[
820 yy::]b4_parser_class_name
[::check_
[] =
825 /* STOS_[STATE-NUM] -- The (internal number of the) accessing
826 symbol of state STATE-NUM. */
827 const ]b4_int_type_for([b4_stos
])[
828 yy::]b4_parser_class_name
[::stos_
[] =
834 /* TOKEN_NUMBER_[YYLEX-NUM] -- Internal token number corresponding
836 const ]b4_int_type_for([b4_toknum
])[
837 yy::]b4_parser_class_name
[::token_number_
[] =
843 /* YYR1[YYN] -- Symbol number of symbol that rule YYN derives. */
844 const ]b4_int_type_for([b4_r1
])[
845 yy::]b4_parser_class_name
[::r1_
[] =
850 /* YYR2[YYN] -- Number of symbols composing right hand side of rule YYN. */
851 const ]b4_int_type_for([b4_r2
])[
852 yy::]b4_parser_class_name
[::r2_
[] =
857 #if YYDEBUG || YYERROR_VERBOSE
858 /* YYTNAME[SYMBOL-NUM] -- String name of the symbol SYMBOL-NUM.
859 First, the terminals, then, starting at NTOKENS_, nonterminals. */
861 const yy::]b4_parser_class_name
[::name_
[] =
868 /* YYRHS -- A `-1'-separated list of the rules' RHS. */
869 const yy::]b4_parser_class_name
[::RhsNumberType
870 yy::]b4_parser_class_name
[::rhs_
[] =
875 /* YYPRHS[YYN] -- Index of the first RHS symbol of rule number YYN in
877 const ]b4_int_type_for([b4_prhs
])[
878 yy::]b4_parser_class_name
[::prhs_
[] =
883 /* YYRLINE[YYN] -- source line where rule number YYN was defined. */
884 const ]b4_int_type_for([b4_rline
])[
885 yy::]b4_parser_class_name
[::rline_
[] =
890 /** Print the state stack from its BOTTOM up to its TOP (included). */
893 yy::]b4_parser_class_name
[::stack_print_ ()
895 cdebug_
<< "Stack now";
896 for (StateStack::const_iterator i
= state_stack_
.begin ();
897 i
!= state_stack_
.end (); ++i
)
898 cdebug_
<< ' ' << *i
;
899 cdebug_
<< std::endl
;
902 /** Report that the YYRULE is going to be reduced. */
905 yy::]b4_parser_class_name
[::reduce_print_ (int yyrule
)
907 unsigned int yylno
= rline_
[yyrule
];
908 /* Print the symbols being reduced, and their result. */
909 cdebug_
<< "Reducing stack by rule " << n_
- 1 << " (line " << yylno
<< "), ";
910 for (]b4_int_type_for([b4_prhs
])[ i
= prhs_
[n_
];
912 cdebug_
<< name_
[rhs_
[i
]] << ' ';
913 cdebug_
<< "-> " << name_
[r1_
[n_
]] << std::endl
;
917 /* YYTRANSLATE(YYLEX) -- Bison symbol number corresponding to YYLEX. */
918 yy::]b4_parser_class_name
[::TokenNumberType
919 yy::]b4_parser_class_name
[::translate_ (int token
)
922 const TokenNumberType
927 if ((unsigned int) token
<= user_token_number_max_
)
928 return translate_table
[token
];
933 const int yy::]b4_parser_class_name
[::eof_
= 0;
934 const int yy::]b4_parser_class_name
[::last_
= ]b4_last
[;
935 const int yy::]b4_parser_class_name
[::nnts_
= ]b4_nterms_number
[;
936 const int yy::]b4_parser_class_name
[::empty_
= -2;
937 const int yy::]b4_parser_class_name
[::final_
= ]b4_final_state_number
[;
938 const int yy::]b4_parser_class_name
[::terror_
= 1;
939 const int yy::]b4_parser_class_name
[::errcode_
= 256;
940 const int yy::]b4_parser_class_name
[::ntokens_
= ]b4_tokens_number
[;
942 const unsigned int yy::]b4_parser_class_name
[::user_token_number_max_
= ]b4_user_token_number_max
[;
943 const yy::]b4_parser_class_name
[::TokenNumberType
yy::]b4_parser_class_name
[::undef_token_
= ]b4_undef_token_number
[;
948 b4_copyright([Stack handling
for Bison C
++ parsers
], [2002, 2003, 2004])[
950 #ifndef BISON_STACK_HH
951 # define BISON_STACK_HH
957 template < class T
, class S
= std::deque
< T
> >
962 // Hide our reversed order.
963 typedef typename
S::reverse_iterator iterator
;
964 typedef typename
S::const_reverse_iterator const_iterator
;
970 Stack (unsigned int n
) : seq_ (n
)
976 operator [] (unsigned int i
)
983 operator [] (unsigned int i
) const
997 pop (unsigned int n
= 1)
1007 return seq_
.size ();
1010 inline const_iterator
begin () const { return seq_
.rbegin (); }
1011 inline const_iterator
end () const { return seq_
.rend (); }
1018 template < class T
, class S
= Stack
< T
> >
1023 Slice (const S
& stack
,
1024 unsigned int range
) : stack_ (stack
),
1031 operator [] (unsigned int i
) const
1033 return stack_
[range_
- i
];
1039 unsigned int range_
;
1043 #endif // not BISON_STACK_HH]
1046 b4_copyright([Position
class for Bison C
++ parsers
], [2002, 2003, 2004])[
1049 ** \file position.hh
1050 ** Define the Location class.
1053 #ifndef BISON_POSITION_HH
1054 # define BISON_POSITION_HH
1056 # include <iostream>
1061 /** \brief Abstract a Position. */
1065 /** \brief Initial column number. */
1066 static const unsigned int initial_column
= 0;
1067 /** \brief Initial line number. */
1068 static const unsigned int initial_line
= 1;
1070 /** \name Ctor & dtor.
1073 /** \brief Construct a Position. */
1076 line (initial_line
),
1077 column (initial_column
)
1083 /** \name Line and Column related manipulators
1086 /** \brief (line related) Advance to the COUNT next lines. */
1087 inline void lines (int count
= 1)
1089 column
= initial_column
;
1093 /** \brief (column related) Advance to the COUNT next columns. */
1094 inline void columns (int count
= 1)
1096 int leftmost
= initial_column
;
1097 int current
= column
;
1098 if (leftmost
<= current
+ count
)
1101 column
= initial_column
;
1106 /** \brief File name to which this position refers. */
1107 std::string filename
;
1108 /** \brief Current line number. */
1110 /** \brief Current column number. */
1111 unsigned int column
;
1114 /** \brief Add and assign a Position. */
1115 inline const Position
&
1116 operator+= (Position
& res
, const int width
)
1118 res
.columns (width
);
1122 /** \brief Add two Position objects. */
1123 inline const Position
1124 operator+ (const Position
& begin
, const int width
)
1126 Position res
= begin
;
1127 return res
+= width
;
1130 /** \brief Add and assign a Position. */
1131 inline const Position
&
1132 operator-= (Position
& res
, const int width
)
1134 return res
+= -width
;
1137 /** \brief Add two Position objects. */
1138 inline const Position
1139 operator- (const Position
& begin
, const int width
)
1141 return begin
+ -width
;
1144 /** \brief Intercept output stream redirection.
1145 ** \param ostr the destination output stream
1146 ** \param pos a reference to the Position to redirect
1148 inline std::ostream
&
1149 operator<< (std::ostream
& ostr
, const Position
& pos
)
1151 if (!pos
.filename
.empty ())
1152 ostr
<< pos
.filename
<< ':';
1153 return ostr
<< pos
.line
<< '.' << pos
.column
;
1157 #endif // not BISON_POSITION_HH]
1159 b4_copyright([Location
class for Bison C
++ parsers
], [2002, 2003, 2004])[
1162 ** \file location.hh
1163 ** Define the Location class.
1166 #ifndef BISON_LOCATION_HH
1167 # define BISON_LOCATION_HH
1169 # include <iostream>
1171 # include "position.hh"
1176 /** \brief Abstract a Location. */
1179 /** \name Ctor & dtor.
1182 /** \brief Construct a Location. */
1191 /** \name Line and Column related manipulators
1194 /** \brief Reset initial location to final location. */
1195 inline void step (void)
1200 /** \brief Extend the current location to the COUNT next columns. */
1201 inline void columns (unsigned int count
= 1)
1206 /** \brief Extend the current location to the COUNT next lines. */
1207 inline void lines (unsigned int count
= 1)
1215 /** \brief Beginning of the located region. */
1217 /** \brief End of the located region. */
1221 /** \brief Join two Location objects to create a Location. */
1222 inline const Location
operator+ (const Location
& begin
, const Location
& end
)
1224 Location res
= begin
;
1229 /** \brief Add two Location objects */
1230 inline const Location
operator+ (const Location
& begin
, unsigned int width
)
1232 Location res
= begin
;
1233 res
.columns (width
);
1237 /** \brief Add and assign a Location */
1238 inline Location
&operator+= (Location
& res
, unsigned int width
)
1240 res
.columns (width
);
1244 /** \brief Intercept output stream redirection.
1245 ** \param ostr the destination output stream
1246 ** \param loc a reference to the Location to redirect
1248 ** Avoid duplicate information.
1250 inline std::ostream
& operator<< (std::ostream
& ostr
, const Location
& loc
)
1252 Position last
= loc
.end
- 1;
1254 if (loc
.begin
.filename
!= last
.filename
)
1255 ostr
<< '-' << last
;
1256 else if (loc
.begin
.line
!= last
.line
)
1257 ostr
<< '-' << last
.line
<< '.' << last
.column
;
1258 else if (loc
.begin
.column
!= last
.column
)
1259 ostr
<< '-' << last
.column
;
1265 #endif // not BISON_LOCATION_HH]