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
],
117 # b4_cxx_destruct_def(IGNORED-ARGUMENTS)
118 # --------------------------------------
119 # Declare the destruct_ method.
120 m4_define([b4_cxx_destruct_def
],
122 yy::b4_parser_class_name::destruct_ (int yytype
, SemanticType
*yyvaluep
, LocationType
*yylocationp
)[]dnl
125 # We do want M4 expansion after # for CPP macros.
128 m4_if(b4_defines_flag
, 0, [],
129 [@output @output_header_name@
130 b4_copyright([C
++ Skeleton parser
for LALR(1) parsing with Bison
],
132 /* FIXME: This is wrong, we want computed header guards.
133 I don't know why the macros are missing now. :( */
134 #ifndef PARSER_HEADER_H
135 # define PARSER_HEADER_H
138 #include "location.hh"
143 /* Using locations. */
144 #define YYLSP_NEEDED ]b4_locations_flag[
146 ]b4_token_defines(b4_tokens
)[
148 /* Copy the first part of user declarations. */
151 ]/* Line __line__ of lalr1.cc. */
152 b4_syncline([@oline@
], [@ofile@
])[
154 /* Enabling traces. */
156 # define YYDEBUG ]b4_debug[
159 /* Enabling verbose error message. */
160 #ifndef YYERROR_VERBOSE
161 # define YYERROR_VERBOSE ]b4_error_verbose[
165 ]m4_ifdef([b4_stype
],
166 [b4_syncline([b4_stype_line
], [b4_filename
])
167 typedef union b4_stype yystype
;
168 /* Line __line__ of lalr1.cc. */
169 b4_syncline([@oline@
], [@ofile@
])],
170 [typedef int yystype
;])[
171 # define YYSTYPE yystype
174 /* Copy the second part of user declarations. */
177 ]/* Line __line__ of lalr1.cc. */
178 b4_syncline([@oline@
], [@ofile@
])[
179 #ifndef YYLLOC_DEFAULT
180 # define YYLLOC_DEFAULT(Current, Rhs, N) \
181 ((Current).end = Rhs[N].end)
186 class ]b4_parser_class_name
[;
188 template < typename P
>
194 struct Traits
< ]b4_parser_class_name
[ >
196 typedef ]b4_int_type_for([b4_translate
])[ TokenNumberType
;
197 typedef ]b4_int_type_for([b4_rhs
])[ RhsNumberType
;
198 typedef int StateType
;
199 typedef yystype SemanticType
;
200 typedef ]b4_location_type
[ LocationType
;
206 class ]b4_parser_class_name b4_inherit
[
210 typedef Traits
< ]b4_parser_class_name
[ >::TokenNumberType TokenNumberType
;
211 typedef Traits
< ]b4_parser_class_name
[ >::RhsNumberType RhsNumberType
;
212 typedef Traits
< ]b4_parser_class_name
[ >::StateType StateType
;
213 typedef Traits
< ]b4_parser_class_name
[ >::SemanticType SemanticType
;
214 typedef Traits
< ]b4_parser_class_name
[ >::LocationType LocationType
;
216 typedef Stack
< StateType
> StateStack
;
217 typedef Stack
< SemanticType
> SemanticStack
;
218 typedef Stack
< LocationType
> LocationStack
;
221 ]b4_parser_class_name
[ (bool debug
,
222 LocationType initlocation
][]b4_param
[]b4_parse_param_decl
[) :
223 ]b4_constructor
[][debug_ (debug
),
225 initlocation_ (initlocation
)]b4_parse_param_cons
[
227 ]b4_parser_class_name
[ (bool debug
][]b4_param
[]b4_parse_param_decl
[) :
228 ]b4_constructor
[][debug_ (debug
),
229 cdebug_ (std::cerr
)]b4_parse_param_cons
[
234 virtual ~]b4_parser_class_name
[ ()
238 virtual int parse ();
242 virtual void lex_ ();
243 virtual void error_ ();
244 virtual void print_ ();
245 virtual void report_syntax_error_ ();
248 StateStack state_stack_
;
249 SemanticStack semantic_stack_
;
250 LocationStack location_stack_
;
253 static const ]b4_int_type_for([b4_pact
])[ pact_
[];
254 static const ]b4_int_type(b4_pact_ninf
, b4_pact_ninf
)[ pact_ninf_
;
255 static const ]b4_int_type_for([b4_defact
])[ defact_
[];
256 static const ]b4_int_type_for([b4_pgoto
])[ pgoto_
[];
257 static const ]b4_int_type_for([b4_defgoto
])[ defgoto_
[];
258 static const ]b4_int_type_for([b4_table
])[ table_
[];
259 static const ]b4_int_type(b4_table_ninf
, b4_table_ninf
)[ table_ninf_
;
260 static const ]b4_int_type_for([b4_check
])[ check_
[];
261 static const ]b4_int_type_for([b4_stos
])[ stos_
[];
262 static const ]b4_int_type_for([b4_r1
])[ r1_
[];
263 static const ]b4_int_type_for([b4_r2
])[ r2_
[];
265 #if YYDEBUG || YYERROR_VERBOSE
266 static const char* const name_
[];
269 /* More tables, for debugging. */
271 static const RhsNumberType rhs_
[];
272 static const ]b4_int_type_for([b4_prhs
])[ prhs_
[];
273 static const ]b4_int_type_for([b4_rline
])[ rline_
[];
274 static const ]b4_int_type_for([b4_toknum
])[ token_number_
[];
275 virtual void reduce_print_ (int yyrule
);
276 virtual void stack_print_ ();
279 /* Even more tables. */
280 static inline TokenNumberType
translate_ (int token
);
281 static inline void destruct_ (int yytype
, SemanticType
*yyvaluep
,
282 LocationType
*yylocationp
);
285 static const int eof_
;
286 /* LAST_ -- Last index in TABLE_. */
287 static const int last_
;
288 static const int nnts_
;
289 static const int empty_
;
290 static const int final_
;
291 static const int terror_
;
292 static const int errcode_
;
293 static const int ntokens_
;
294 static const unsigned int user_token_number_max_
;
295 static const TokenNumberType undef_token_
;
302 /* Error handling. */
308 std::ostream
&cdebug_
;
310 /* Lookahead and lookahead in internal form. */
317 /* Semantic value and location of lookahead token. */
319 LocationType location
;
320 /* Beginning of the last erroneous token popped off. */
321 Position error_start_
;
327 /* Initial location. */
328 LocationType initlocation_
;
329 ]b4_parse_param_vars
[
333 #endif /* ! defined PARSER_HEADER_H */]
335 @output @output_parser_name@
336 b4_copyright([C
++ Skeleton parser
for LALR(1) parsing with Bison
],
339 m4_if(b4_defines_flag
, 0, [], [#include @output_header_name@])[
341 /* Enable debugging if requested. */
343 # define YYCDEBUG if (debug_) cdebug_
344 # define YY_REDUCE_PRINT(Rule) \
347 reduce_print_ (Rule); \
349 # define YY_STACK_PRINT() \
355 # define YYCDEBUG if (0) cdebug_
356 # define YY_REDUCE_PRINT(Rule)
357 # define YY_STACK_PRINT()
358 #endif /* !YYDEBUG */
360 #define YYACCEPT goto yyacceptlab
361 #define YYABORT goto yyabortlab
362 #define YYERROR goto yyerrorlab
364 ]b4_yydestruct_generate([b4_cxx_destruct_def
])[
367 yy::]b4_parser_class_name
[::parse ()
372 /* Initialize the stacks. The initial state will be pushed in
373 yynewstate, since the latter expects the semantical and the
374 location values to have been already stored, initialize these
375 stacks with a primary value. */
376 state_stack_
= StateStack (0);
377 semantic_stack_
= SemanticStack (1);
378 location_stack_
= LocationStack (1);
384 location
= initlocation_
;
386 YYCDEBUG
<< "Starting parse" << std::endl
;
390 state_stack_
.push (state_
);
391 YYCDEBUG
<< "Entering state " << state_
<< std::endl
;
397 /* Try to take a decision without lookahead. */
399 if (n_
== pact_ninf_
)
402 /* Read a lookahead token. */
403 if (looka_
== empty_
)
405 YYCDEBUG
<< "Reading a token: ";
409 /* Convert token to internal form. */
414 YYCDEBUG
<< "Now at end of input." << std::endl
;
418 ilooka_
= translate_ (looka_
);
422 YYCDEBUG
<< "Next token is " << looka_
423 << " (" << name_
[ilooka_
];
425 YYCDEBUG
<< ')' << std::endl
;
430 /* If the proper action on seeing token YYTOKEN is to reduce or to
431 detect an error, take that action. */
433 if (n_
< 0 || last_
< n_
|| check_
[n_
] != ilooka_
)
436 /* Reduce or error. */
440 if (n_
== table_ninf_
)
455 /* Shift the lookahead token. */
457 YYCDEBUG
<< "Shifting token " << looka_
458 << " (" << name_
[ilooka_
] << "), ";
461 /* Discard the token being shifted unless it is eof. */
465 semantic_stack_
.push (value
);
466 location_stack_
.push (location
);
468 /* Count tokens shifted since error; after three, turn off error
476 /*-----------------------------------------------------------.
477 | yydefault -- do the default action for the current state. |
478 `-----------------------------------------------------------*/
480 n_
= defact_
[state_
];
485 /*-----------------------------.
486 | yyreduce -- Do a reduction. |
487 `-----------------------------*/
490 /* If LEN_ is nonzero, implement the default value of the action:
491 `$$ = $1'. Otherwise, use the top of the stack.
493 Otherwise, the following line sets YYVAL to garbage.
494 This behavior is undocumented and Bison
495 users should not rely upon it. */
498 yyval
= semantic_stack_
[len_
- 1];
499 yyloc
= location_stack_
[len_
- 1];
503 yyval
= semantic_stack_
[0];
504 yyloc
= location_stack_
[0];
509 Slice
< LocationType
, LocationStack
> slice (location_stack_
, len_
);
510 YYLLOC_DEFAULT (yyloc
, slice
, len_
);
512 YY_REDUCE_PRINT (n_
);
518 ]/* Line __line__ of lalr1.cc. */
519 b4_syncline([@oline@
], [@ofile@
])[
521 state_stack_
.pop (len_
);
522 semantic_stack_
.pop (len_
);
523 location_stack_
.pop (len_
);
527 semantic_stack_
.push (yyval
);
528 location_stack_
.push (yyloc
);
530 /* Shift the result of the reduction. */
532 state_
= pgoto_
[n_
- ntokens_
] + state_stack_
[0];
533 if (0 <= state_
&& state_
<= last_
&& check_
[state_
] == state_stack_
[0])
534 state_
= table_
[state_
];
536 state_
= defgoto_
[n_
- ntokens_
];
539 /*------------------------------------.
540 | yyerrlab -- here on detecting error |
541 `------------------------------------*/
543 /* If not already recovering from an error, report this error. */
544 report_syntax_error_ ();
546 error_start_
= location
.begin
;
549 /* If just tried and failed to reuse lookahead token after an
550 error, discard it. */
552 /* Return failure if at end of input. */
555 /* If at end of input, pop the error token,
556 then the rest of the stack, then return failure. */
560 error_start_
= location_stack_
[0].begin
;
562 semantic_stack_
.pop ();
563 location_stack_
.pop ();
564 if (state_stack_
.height () == 1)
566 // YYDSYMPRINTF ("Error: popping", yystos[*yyssp], yyvsp, yylsp);
567 destruct_ (stos_
[state_stack_
[0]],
569 &location_stack_
[0]);
575 YYCDEBUG
<< "Discarding token " << looka_
576 << " (" << name_
[ilooka_
] << ")." << std::endl
;
578 destruct_ (ilooka_
, &value
, &location
);
583 /* Else will try to reuse lookahead token after shifting the error
588 /*---------------------------------------------------.
589 | yyerrorlab -- error raised explicitly by YYERROR. |
590 `---------------------------------------------------*/
594 /* Pacify GCC when the user code never invokes YYERROR and the label
595 yyerrorlab therefore never appears in user code. */
600 state_stack_
.pop (len_
);
601 semantic_stack_
.pop (len_
);
602 error_start_
= location_stack_
[len_
- 1].begin
;
603 location_stack_
.pop (len_
);
604 state_
= state_stack_
[0];
607 /*-------------------------------------------------------------.
608 | yyerrlab1 -- common code for both syntax error and YYERROR. |
609 `-------------------------------------------------------------*/
611 errstatus_
= 3; /* Each real token shifted decrements this. */
616 if (n_
!= pact_ninf_
)
619 if (0 <= n_
&& n_
<= last_
&& check_
[n_
] == terror_
)
627 /* Pop the current state because it cannot handle the error token. */
628 if (state_stack_
.height () == 1)
634 if (stos_
[state_
] < ntokens_
)
636 YYCDEBUG
<< "Error: popping token "
637 << token_number_
[stos_
[state_
]]
638 << " (" << name_
[stos_
[state_
]];
640 YYPRINT (stderr
, token_number_
[stos_
[state_
]],
641 semantic_stack_
.top ());
643 YYCDEBUG
<< ')' << std::endl
;
647 YYCDEBUG
<< "Error: popping nonterminal ("
648 << name_
[stos_
[state_
]] << ')' << std::endl
;
652 destruct_ (stos_
[state_
], &semantic_stack_
[0], &location_stack_
[0]);
653 error_start_
= location_stack_
[0].begin
;
656 semantic_stack_
.pop ();
657 location_stack_
.pop ();
658 state_
= state_stack_
[0];
665 YYCDEBUG
<< "Shifting error token, ";
669 errloc
.begin
= error_start_
;
670 errloc
.end
= location
.end
;
671 semantic_stack_
.push (value
);
672 location_stack_
.push (errloc
);
688 yy::]b4_parser_class_name
[::lex_ ()
691 looka_
= yylex (&value
, &location
);
693 looka_
= yylex (&value
);
697 /** Generate an error message, and invoke yyerror. */
699 yy::]b4_parser_class_name
[::report_syntax_error_ ()
701 /* If not already recovering from an error, report this error. */
708 if (pact_ninf_
< n_
&& n_
< last_
)
710 message
= "syntax error, unexpected ";
711 message
+= name_
[ilooka_
];
714 for (int x
= (n_
< 0 ? -n_
: 0); x
< ntokens_
+ nnts_
; ++x
)
715 if (check_
[x
+ n_
] == x
&& x
!= terror_
)
720 for (int x
= (n_
< 0 ? -n_
: 0); x
< ntokens_
+ nnts_
; ++x
)
721 if (check_
[x
+ n_
] == x
&& x
!= terror_
)
723 message
+= (!count
++) ? ", expecting " : " or ";
731 message
= "syntax error";
737 /* YYPACT[STATE-NUM] -- Index in YYTABLE of the portion describing
739 const ]b4_int_type(b4_pact_ninf
, b4_pact_ninf
) yy::b4_parser_class_name::pact_ninf_
= b4_pact_ninf
[;
740 const ]b4_int_type_for([b4_pact
])[
741 yy::]b4_parser_class_name
[::pact_
[] =
746 /* YYDEFACT[S] -- default rule to reduce with in state S when YYTABLE
747 doesn't specify something else to do. Zero means the default is an
749 const ]b4_int_type_for([b4_defact
])[
750 yy::]b4_parser_class_name
[::defact_
[] =
755 /* YYPGOTO[NTERM-NUM]. */
756 const ]b4_int_type_for([b4_pgoto
])[
757 yy::]b4_parser_class_name
[::pgoto_
[] =
762 /* YYDEFGOTO[NTERM-NUM]. */
763 const ]b4_int_type_for([b4_defgoto
])[
764 yy::]b4_parser_class_name
[::defgoto_
[] =
769 /* YYTABLE[YYPACT[STATE-NUM]]. What to do in state STATE-NUM. If
770 positive, shift that token. If negative, reduce the rule which
771 number is the opposite. If zero, do what YYDEFACT says. */
772 const ]b4_int_type(b4_table_ninf
, b4_table_ninf
) yy::b4_parser_class_name::table_ninf_
= b4_table_ninf
[;
773 const ]b4_int_type_for([b4_table
])[
774 yy::]b4_parser_class_name
[::table_
[] =
780 const ]b4_int_type_for([b4_check
])[
781 yy::]b4_parser_class_name
[::check_
[] =
786 /* STOS_[STATE-NUM] -- The (internal number of the) accessing
787 symbol of state STATE-NUM. */
788 const ]b4_int_type_for([b4_stos
])[
789 yy::]b4_parser_class_name
[::stos_
[] =
795 /* TOKEN_NUMBER_[YYLEX-NUM] -- Internal token number corresponding
797 const ]b4_int_type_for([b4_toknum
])[
798 yy::]b4_parser_class_name
[::token_number_
[] =
804 /* YYR1[YYN] -- Symbol number of symbol that rule YYN derives. */
805 const ]b4_int_type_for([b4_r1
])[
806 yy::]b4_parser_class_name
[::r1_
[] =
811 /* YYR2[YYN] -- Number of symbols composing right hand side of rule YYN. */
812 const ]b4_int_type_for([b4_r2
])[
813 yy::]b4_parser_class_name
[::r2_
[] =
818 #if YYDEBUG || YYERROR_VERBOSE
819 /* YYTNAME[SYMBOL-NUM] -- String name of the symbol SYMBOL-NUM.
820 First, the terminals, then, starting at YYNTOKENS, nonterminals. */
822 const yy::]b4_parser_class_name
[::name_
[] =
829 /* YYRHS -- A `-1'-separated list of the rules' RHS. */
830 const yy::]b4_parser_class_name
[::RhsNumberType
831 yy::]b4_parser_class_name
[::rhs_
[] =
836 /* YYPRHS[YYN] -- Index of the first RHS symbol of rule number YYN in
838 const ]b4_int_type_for([b4_prhs
])[
839 yy::]b4_parser_class_name
[::prhs_
[] =
844 /* YYRLINE[YYN] -- source line where rule number YYN was defined. */
845 const ]b4_int_type_for([b4_rline
])[
846 yy::]b4_parser_class_name
[::rline_
[] =
851 /** Print the state stack from its BOTTOM up to its TOP (included). */
854 yy::]b4_parser_class_name
[::stack_print_ ()
856 cdebug_
<< "state stack now";
857 for (StateStack::ConstIterator i
= state_stack_
.begin ();
858 i
!= state_stack_
.end (); ++i
)
859 cdebug_
<< ' ' << *i
;
860 cdebug_
<< std::endl
;
863 /** Report that the YYRULE is going to be reduced. */
866 yy::]b4_parser_class_name
[::reduce_print_ (int yyrule
)
868 unsigned int yylno
= rline_
[yyrule
];
869 /* Print the symbols being reduced, and their result. */
870 cdebug_
<< "Reducing via rule " << n_
- 1 << " (line " << yylno
<< "), ";
871 for (]b4_int_type_for([b4_prhs
])[ i
= prhs_
[n_
];
873 cdebug_
<< name_
[rhs_
[i
]] << ' ';
874 cdebug_
<< "-> " << name_
[r1_
[n_
]] << std::endl
;
878 /* YYTRANSLATE(YYLEX) -- Bison symbol number corresponding to YYLEX. */
879 yy::]b4_parser_class_name
[::TokenNumberType
880 yy::]b4_parser_class_name
[::translate_ (int token
)
883 const TokenNumberType
888 if ((unsigned int) token
<= user_token_number_max_
)
889 return translate_table
[token
];
894 const int yy::]b4_parser_class_name
[::eof_
= 0;
895 const int yy::]b4_parser_class_name
[::last_
= ]b4_last
[;
896 const int yy::]b4_parser_class_name
[::nnts_
= ]b4_nterms_number
[;
897 const int yy::]b4_parser_class_name
[::empty_
= -2;
898 const int yy::]b4_parser_class_name
[::final_
= ]b4_final_state_number
[;
899 const int yy::]b4_parser_class_name
[::terror_
= 1;
900 const int yy::]b4_parser_class_name
[::errcode_
= 256;
901 const int yy::]b4_parser_class_name
[::ntokens_
= ]b4_tokens_number
[;
903 const unsigned int yy::]b4_parser_class_name
[::user_token_number_max_
= ]b4_user_token_number_max
[;
904 const yy::]b4_parser_class_name
[::TokenNumberType
yy::]b4_parser_class_name
[::undef_token_
= ]b4_undef_token_number
[;
909 b4_copyright([Stack handling
for Bison C
++ parsers
], [2002, 2003, 2004])[
911 #ifndef BISON_STACK_HH
912 # define BISON_STACK_HH
918 template < class T
, class S
= std::deque
< T
> >
923 typedef typename
S::iterator Iterator
;
924 typedef typename
S::const_iterator ConstIterator
;
930 Stack (unsigned int n
) : seq_ (n
)
936 operator [] (unsigned int i
)
943 operator [] (unsigned int i
) const
957 pop (unsigned int n
= 1)
970 inline ConstIterator
begin () const { return seq_
.begin (); }
971 inline ConstIterator
end () const { return seq_
.end (); }
978 template < class T
, class S
= Stack
< T
> >
983 Slice (const S
& stack
,
984 unsigned int range
) : stack_ (stack
),
991 operator [] (unsigned int i
) const
993 return stack_
[range_
- i
];
1003 #endif // not BISON_STACK_HH]
1006 b4_copyright([Position
class for Bison C
++ parsers
], [2002, 2003, 2004])[
1009 ** \file position.hh
1010 ** Define the Location class.
1013 #ifndef BISON_POSITION_HH
1014 # define BISON_POSITION_HH
1016 # include <iostream>
1021 /** \brief Abstract a Position. */
1025 /** \brief Initial column number. */
1026 static const unsigned int initial_column
= 0;
1027 /** \brief Initial line number. */
1028 static const unsigned int initial_line
= 1;
1030 /** \name Ctor & dtor.
1033 /** \brief Construct a Position. */
1036 line (initial_line
),
1037 column (initial_column
)
1043 /** \name Line and Column related manipulators
1046 /** \brief (line related) Advance to the COUNT next lines. */
1047 inline void lines (int count
= 1)
1049 column
= initial_column
;
1053 /** \brief (column related) Advance to the COUNT next columns. */
1054 inline void columns (int count
= 1)
1056 int leftmost
= initial_column
;
1057 int current
= column
;
1058 if (leftmost
<= current
+ count
)
1061 column
= initial_column
;
1066 /** \brief File name to which this position refers. */
1067 std::string filename
;
1068 /** \brief Current line number. */
1070 /** \brief Current column number. */
1071 unsigned int column
;
1074 /** \brief Add and assign a Position. */
1075 inline const Position
&
1076 operator+= (Position
& res
, const int width
)
1078 res
.columns (width
);
1082 /** \brief Add two Position objects. */
1083 inline const Position
1084 operator+ (const Position
& begin
, const int width
)
1086 Position res
= begin
;
1087 return res
+= width
;
1090 /** \brief Add and assign a Position. */
1091 inline const Position
&
1092 operator-= (Position
& res
, const int width
)
1094 return res
+= -width
;
1097 /** \brief Add two Position objects. */
1098 inline const Position
1099 operator- (const Position
& begin
, const int width
)
1101 return begin
+ -width
;
1104 /** \brief Intercept output stream redirection.
1105 ** \param ostr the destination output stream
1106 ** \param pos a reference to the Position to redirect
1108 inline std::ostream
&
1109 operator<< (std::ostream
& ostr
, const Position
& pos
)
1111 if (!pos
.filename
.empty ())
1112 ostr
<< pos
.filename
<< ':';
1113 return ostr
<< pos
.line
<< '.' << pos
.column
;
1117 #endif // not BISON_POSITION_HH]
1119 b4_copyright([Location
class for Bison C
++ parsers
], [2002, 2003, 2004])[
1122 ** \file location.hh
1123 ** Define the Location class.
1126 #ifndef BISON_LOCATION_HH
1127 # define BISON_LOCATION_HH
1129 # include <iostream>
1131 # include "position.hh"
1136 /** \brief Abstract a Location. */
1139 /** \name Ctor & dtor.
1142 /** \brief Construct a Location. */
1151 /** \name Line and Column related manipulators
1154 /** \brief Reset initial location to final location. */
1155 inline void step (void)
1160 /** \brief Extend the current location to the COUNT next columns. */
1161 inline void columns (unsigned int count
= 1)
1166 /** \brief Extend the current location to the COUNT next lines. */
1167 inline void lines (unsigned int count
= 1)
1175 /** \brief Beginning of the located region. */
1177 /** \brief End of the located region. */
1181 /** \brief Join two Location objects to create a Location. */
1182 inline const Location
operator+ (const Location
& begin
, const Location
& end
)
1184 Location res
= begin
;
1189 /** \brief Add two Location objects */
1190 inline const Location
operator+ (const Location
& begin
, unsigned int width
)
1192 Location res
= begin
;
1193 res
.columns (width
);
1197 /** \brief Add and assign a Location */
1198 inline Location
&operator+= (Location
& res
, unsigned int width
)
1200 res
.columns (width
);
1204 /** \brief Intercept output stream redirection.
1205 ** \param ostr the destination output stream
1206 ** \param loc a reference to the Location to redirect
1208 ** Avoid duplicate information.
1210 inline std::ostream
& operator<< (std::ostream
& ostr
, const Location
& loc
)
1212 Position last
= loc
.end
- 1;
1214 if (loc
.begin
.filename
!= last
.filename
)
1215 ostr
<< '-' << last
;
1216 else if (loc
.begin
.line
!= last
.line
)
1217 ostr
<< '-' << last
.line
<< '.' << last
.column
;
1218 else if (loc
.begin
.column
!= last
.column
)
1219 ostr
<< '-' << last
.column
;
1225 #endif // not BISON_LOCATION_HH]