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 typedef YYSTYPE yystype
;
167 ]m4_ifdef([b4_stype
],
168 [b4_syncline([b4_stype_line
], [b4_filename
])
169 typedef union b4_stype yystype
;
170 /* Line __line__ of lalr1.cc. */
171 b4_syncline([@oline@
], [@ofile@
])],
172 [typedef int yystype
;])[
173 # define YYSTYPE yystype
176 /* Copy the second part of user declarations. */
179 ]/* Line __line__ of lalr1.cc. */
180 b4_syncline([@oline@
], [@ofile@
])[
181 #ifndef YYLLOC_DEFAULT
182 # define YYLLOC_DEFAULT(Current, Rhs, N) \
183 ((Current).end = Rhs[N].end)
188 class ]b4_parser_class_name
[;
190 template < typename P
>
196 struct Traits
< ]b4_parser_class_name
[ >
198 typedef ]b4_int_type_for([b4_translate
])[ TokenNumberType
;
199 typedef ]b4_int_type_for([b4_rhs
])[ RhsNumberType
;
200 typedef int StateType
;
201 typedef yystype SemanticType
;
202 typedef ]b4_location_type
[ LocationType
;
208 class ]b4_parser_class_name b4_inherit
[
212 typedef Traits
< ]b4_parser_class_name
[ >::TokenNumberType TokenNumberType
;
213 typedef Traits
< ]b4_parser_class_name
[ >::RhsNumberType RhsNumberType
;
214 typedef Traits
< ]b4_parser_class_name
[ >::StateType StateType
;
215 typedef Traits
< ]b4_parser_class_name
[ >::SemanticType SemanticType
;
216 typedef Traits
< ]b4_parser_class_name
[ >::LocationType LocationType
;
218 typedef Stack
< StateType
> StateStack
;
219 typedef Stack
< SemanticType
> SemanticStack
;
220 typedef Stack
< LocationType
> LocationStack
;
223 ]b4_parser_class_name
[ (bool debug
,
224 LocationType initlocation
][]b4_param
[]b4_parse_param_decl
[) :
225 ]b4_constructor
[][debug_ (debug
),
227 initlocation_ (initlocation
)]b4_parse_param_cons
[
229 ]b4_parser_class_name
[ (bool debug
][]b4_param
[]b4_parse_param_decl
[) :
230 ]b4_constructor
[][debug_ (debug
),
231 cdebug_ (std::cerr
)]b4_parse_param_cons
[
236 virtual ~]b4_parser_class_name
[ ()
240 virtual int parse ();
244 virtual void lex_ ();
245 virtual void error_ ();
246 virtual void print_ ();
247 virtual void report_syntax_error_ ();
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 static inline TokenNumberType
translate_ (int token
);
283 static inline void destruct_ (int yytype
, SemanticType
*yyvaluep
,
284 LocationType
*yylocationp
);
287 static const int eof_
;
288 /* LAST_ -- Last index in TABLE_. */
289 static const int last_
;
290 static const int nnts_
;
291 static const int empty_
;
292 static const int final_
;
293 static const int terror_
;
294 static const int errcode_
;
295 static const int ntokens_
;
296 static const unsigned int user_token_number_max_
;
297 static const TokenNumberType undef_token_
;
304 /* Error handling. */
310 std::ostream
&cdebug_
;
312 /* Look-ahead and look-ahead in internal form. */
319 /* Semantic value and location of look-ahead token. */
321 LocationType location
;
322 /* Beginning of the last erroneous token popped off. */
323 Position error_start_
;
329 /* Initial location. */
330 LocationType initlocation_
;
331 ]b4_parse_param_vars
[
335 #endif /* ! defined PARSER_HEADER_H */]
337 @output @output_parser_name@
338 b4_copyright([C
++ Skeleton parser
for LALR(1) parsing with Bison
],
341 m4_if(b4_defines_flag
, 0, [], [#include @output_header_name@])[
343 /* Enable debugging if requested. */
345 # define YYCDEBUG if (debug_) cdebug_
346 # define YY_REDUCE_PRINT(Rule) \
349 reduce_print_ (Rule); \
351 # define YY_STACK_PRINT() \
357 # define YYCDEBUG if (0) cdebug_
358 # define YY_REDUCE_PRINT(Rule)
359 # define YY_STACK_PRINT()
360 #endif /* !YYDEBUG */
362 #define YYACCEPT goto yyacceptlab
363 #define YYABORT goto yyabortlab
364 #define YYERROR goto yyerrorlab
366 ]b4_yydestruct_generate([b4_cxx_destruct_def
])[
369 yy::]b4_parser_class_name
[::parse ()
374 /* Initialize the stacks. The initial state will be pushed in
375 yynewstate, since the latter expects the semantical and the
376 location values to have been already stored, initialize these
377 stacks with a primary value. */
378 state_stack_
= StateStack (0);
379 semantic_stack_
= SemanticStack (1);
380 location_stack_
= LocationStack (1);
386 location
= initlocation_
;
388 YYCDEBUG
<< "Starting parse" << std::endl
;
392 state_stack_
.push (state_
);
393 YYCDEBUG
<< "Entering state " << state_
<< std::endl
;
399 /* Try to take a decision without look-ahead. */
401 if (n_
== pact_ninf_
)
404 /* Read a look-ahead token. */
405 if (looka_
== empty_
)
407 YYCDEBUG
<< "Reading a token: ";
411 /* Convert token to internal form. */
416 YYCDEBUG
<< "Now at end of input." << std::endl
;
420 ilooka_
= translate_ (looka_
);
424 YYCDEBUG
<< "Next token is " << looka_
425 << " (" << name_
[ilooka_
];
427 YYCDEBUG
<< ')' << std::endl
;
432 /* If the proper action on seeing token YYTOKEN is to reduce or to
433 detect an error, take that action. */
435 if (n_
< 0 || last_
< n_
|| check_
[n_
] != ilooka_
)
438 /* Reduce or error. */
442 if (n_
== table_ninf_
)
457 /* Shift the look-ahead token. */
459 YYCDEBUG
<< "Shifting token " << looka_
460 << " (" << name_
[ilooka_
] << "), ";
463 /* Discard the token being shifted unless it is eof. */
467 semantic_stack_
.push (value
);
468 location_stack_
.push (location
);
470 /* Count tokens shifted since error; after three, turn off error
478 /*-----------------------------------------------------------.
479 | yydefault -- do the default action for the current state. |
480 `-----------------------------------------------------------*/
482 n_
= defact_
[state_
];
487 /*-----------------------------.
488 | yyreduce -- Do a reduction. |
489 `-----------------------------*/
492 /* If LEN_ is nonzero, implement the default value of the action:
493 `$$ = $1'. Otherwise, use the top of the stack.
495 Otherwise, the following line sets YYVAL to garbage.
496 This behavior is undocumented and Bison
497 users should not rely upon it. */
500 yyval
= semantic_stack_
[len_
- 1];
501 yyloc
= location_stack_
[len_
- 1];
505 yyval
= semantic_stack_
[0];
506 yyloc
= location_stack_
[0];
511 Slice
< LocationType
, LocationStack
> slice (location_stack_
, len_
);
512 YYLLOC_DEFAULT (yyloc
, slice
, len_
);
514 YY_REDUCE_PRINT (n_
);
520 ]/* Line __line__ of lalr1.cc. */
521 b4_syncline([@oline@
], [@ofile@
])[
523 state_stack_
.pop (len_
);
524 semantic_stack_
.pop (len_
);
525 location_stack_
.pop (len_
);
529 semantic_stack_
.push (yyval
);
530 location_stack_
.push (yyloc
);
532 /* Shift the result of the reduction. */
534 state_
= pgoto_
[n_
- ntokens_
] + state_stack_
[0];
535 if (0 <= state_
&& state_
<= last_
&& check_
[state_
] == state_stack_
[0])
536 state_
= table_
[state_
];
538 state_
= defgoto_
[n_
- ntokens_
];
541 /*------------------------------------.
542 | yyerrlab -- here on detecting error |
543 `------------------------------------*/
545 /* If not already recovering from an error, report this error. */
546 report_syntax_error_ ();
548 error_start_
= location
.begin
;
551 /* If just tried and failed to reuse look-ahead token after an
552 error, discard it. */
554 /* Return failure if at end of input. */
557 /* If at end of input, pop the error token,
558 then the rest of the stack, then return failure. */
562 error_start_
= location_stack_
[0].begin
;
564 semantic_stack_
.pop ();
565 location_stack_
.pop ();
566 if (state_stack_
.height () == 1)
568 // YYDSYMPRINTF ("Error: popping", yystos[*yyssp], yyvsp, yylsp);
569 destruct_ (stos_
[state_stack_
[0]],
571 &location_stack_
[0]);
577 YYCDEBUG
<< "Discarding token " << looka_
578 << " (" << name_
[ilooka_
] << ")." << std::endl
;
580 destruct_ (ilooka_
, &value
, &location
);
585 /* Else will try to reuse look-ahead token after shifting the error
590 /*---------------------------------------------------.
591 | yyerrorlab -- error raised explicitly by YYERROR. |
592 `---------------------------------------------------*/
596 /* Pacify GCC when the user code never invokes YYERROR and the label
597 yyerrorlab therefore never appears in user code. */
602 state_stack_
.pop (len_
);
603 semantic_stack_
.pop (len_
);
604 error_start_
= location_stack_
[len_
- 1].begin
;
605 location_stack_
.pop (len_
);
606 state_
= state_stack_
[0];
609 /*-------------------------------------------------------------.
610 | yyerrlab1 -- common code for both syntax error and YYERROR. |
611 `-------------------------------------------------------------*/
613 errstatus_
= 3; /* Each real token shifted decrements this. */
618 if (n_
!= pact_ninf_
)
621 if (0 <= n_
&& n_
<= last_
&& check_
[n_
] == terror_
)
629 /* Pop the current state because it cannot handle the error token. */
630 if (state_stack_
.height () == 1)
636 if (stos_
[state_
] < ntokens_
)
638 YYCDEBUG
<< "Error: popping token "
639 << token_number_
[stos_
[state_
]]
640 << " (" << name_
[stos_
[state_
]];
642 YYPRINT (stderr
, token_number_
[stos_
[state_
]],
643 semantic_stack_
.top ());
645 YYCDEBUG
<< ')' << std::endl
;
649 YYCDEBUG
<< "Error: popping nonterminal ("
650 << name_
[stos_
[state_
]] << ')' << std::endl
;
654 destruct_ (stos_
[state_
], &semantic_stack_
[0], &location_stack_
[0]);
655 error_start_
= location_stack_
[0].begin
;
658 semantic_stack_
.pop ();
659 location_stack_
.pop ();
660 state_
= state_stack_
[0];
667 YYCDEBUG
<< "Shifting error token, ";
671 errloc
.begin
= error_start_
;
672 errloc
.end
= location
.end
;
673 semantic_stack_
.push (value
);
674 location_stack_
.push (errloc
);
690 yy::]b4_parser_class_name
[::lex_ ()
693 looka_
= yylex (&value
, &location
);
695 looka_
= yylex (&value
);
699 /** Generate an error message, and invoke yyerror. */
701 yy::]b4_parser_class_name
[::report_syntax_error_ ()
703 /* If not already recovering from an error, report this error. */
710 if (pact_ninf_
< n_
&& n_
< last_
)
712 message
= "syntax error, unexpected ";
713 message
+= name_
[ilooka_
];
716 for (int x
= (n_
< 0 ? -n_
: 0); x
< ntokens_
+ nnts_
; ++x
)
717 if (check_
[x
+ n_
] == x
&& x
!= terror_
)
722 for (int x
= (n_
< 0 ? -n_
: 0); x
< ntokens_
+ nnts_
; ++x
)
723 if (check_
[x
+ n_
] == x
&& x
!= terror_
)
725 message
+= (!count
++) ? ", expecting " : " or ";
733 message
= "syntax error";
739 /* YYPACT[STATE-NUM] -- Index in YYTABLE of the portion describing
741 const ]b4_int_type(b4_pact_ninf
, b4_pact_ninf
) yy::b4_parser_class_name::pact_ninf_
= b4_pact_ninf
[;
742 const ]b4_int_type_for([b4_pact
])[
743 yy::]b4_parser_class_name
[::pact_
[] =
748 /* YYDEFACT[S] -- default rule to reduce with in state S when YYTABLE
749 doesn't specify something else to do. Zero means the default is an
751 const ]b4_int_type_for([b4_defact
])[
752 yy::]b4_parser_class_name
[::defact_
[] =
757 /* YYPGOTO[NTERM-NUM]. */
758 const ]b4_int_type_for([b4_pgoto
])[
759 yy::]b4_parser_class_name
[::pgoto_
[] =
764 /* YYDEFGOTO[NTERM-NUM]. */
765 const ]b4_int_type_for([b4_defgoto
])[
766 yy::]b4_parser_class_name
[::defgoto_
[] =
771 /* YYTABLE[YYPACT[STATE-NUM]]. What to do in state STATE-NUM. If
772 positive, shift that token. If negative, reduce the rule which
773 number is the opposite. If zero, do what YYDEFACT says. */
774 const ]b4_int_type(b4_table_ninf
, b4_table_ninf
) yy::b4_parser_class_name::table_ninf_
= b4_table_ninf
[;
775 const ]b4_int_type_for([b4_table
])[
776 yy::]b4_parser_class_name
[::table_
[] =
782 const ]b4_int_type_for([b4_check
])[
783 yy::]b4_parser_class_name
[::check_
[] =
788 /* STOS_[STATE-NUM] -- The (internal number of the) accessing
789 symbol of state STATE-NUM. */
790 const ]b4_int_type_for([b4_stos
])[
791 yy::]b4_parser_class_name
[::stos_
[] =
797 /* TOKEN_NUMBER_[YYLEX-NUM] -- Internal token number corresponding
799 const ]b4_int_type_for([b4_toknum
])[
800 yy::]b4_parser_class_name
[::token_number_
[] =
806 /* YYR1[YYN] -- Symbol number of symbol that rule YYN derives. */
807 const ]b4_int_type_for([b4_r1
])[
808 yy::]b4_parser_class_name
[::r1_
[] =
813 /* YYR2[YYN] -- Number of symbols composing right hand side of rule YYN. */
814 const ]b4_int_type_for([b4_r2
])[
815 yy::]b4_parser_class_name
[::r2_
[] =
820 #if YYDEBUG || YYERROR_VERBOSE
821 /* YYTNAME[SYMBOL-NUM] -- String name of the symbol SYMBOL-NUM.
822 First, the terminals, then, starting at YYNTOKENS, nonterminals. */
824 const yy::]b4_parser_class_name
[::name_
[] =
831 /* YYRHS -- A `-1'-separated list of the rules' RHS. */
832 const yy::]b4_parser_class_name
[::RhsNumberType
833 yy::]b4_parser_class_name
[::rhs_
[] =
838 /* YYPRHS[YYN] -- Index of the first RHS symbol of rule number YYN in
840 const ]b4_int_type_for([b4_prhs
])[
841 yy::]b4_parser_class_name
[::prhs_
[] =
846 /* YYRLINE[YYN] -- source line where rule number YYN was defined. */
847 const ]b4_int_type_for([b4_rline
])[
848 yy::]b4_parser_class_name
[::rline_
[] =
853 /** Print the state stack from its BOTTOM up to its TOP (included). */
856 yy::]b4_parser_class_name
[::stack_print_ ()
858 cdebug_
<< "state stack now";
859 for (StateStack::ConstIterator i
= state_stack_
.begin ();
860 i
!= state_stack_
.end (); ++i
)
861 cdebug_
<< ' ' << *i
;
862 cdebug_
<< std::endl
;
865 /** Report that the YYRULE is going to be reduced. */
868 yy::]b4_parser_class_name
[::reduce_print_ (int yyrule
)
870 unsigned int yylno
= rline_
[yyrule
];
871 /* Print the symbols being reduced, and their result. */
872 cdebug_
<< "Reducing via rule " << n_
- 1 << " (line " << yylno
<< "), ";
873 for (]b4_int_type_for([b4_prhs
])[ i
= prhs_
[n_
];
875 cdebug_
<< name_
[rhs_
[i
]] << ' ';
876 cdebug_
<< "-> " << name_
[r1_
[n_
]] << std::endl
;
880 /* YYTRANSLATE(YYLEX) -- Bison symbol number corresponding to YYLEX. */
881 yy::]b4_parser_class_name
[::TokenNumberType
882 yy::]b4_parser_class_name
[::translate_ (int token
)
885 const TokenNumberType
890 if ((unsigned int) token
<= user_token_number_max_
)
891 return translate_table
[token
];
896 const int yy::]b4_parser_class_name
[::eof_
= 0;
897 const int yy::]b4_parser_class_name
[::last_
= ]b4_last
[;
898 const int yy::]b4_parser_class_name
[::nnts_
= ]b4_nterms_number
[;
899 const int yy::]b4_parser_class_name
[::empty_
= -2;
900 const int yy::]b4_parser_class_name
[::final_
= ]b4_final_state_number
[;
901 const int yy::]b4_parser_class_name
[::terror_
= 1;
902 const int yy::]b4_parser_class_name
[::errcode_
= 256;
903 const int yy::]b4_parser_class_name
[::ntokens_
= ]b4_tokens_number
[;
905 const unsigned int yy::]b4_parser_class_name
[::user_token_number_max_
= ]b4_user_token_number_max
[;
906 const yy::]b4_parser_class_name
[::TokenNumberType
yy::]b4_parser_class_name
[::undef_token_
= ]b4_undef_token_number
[;
911 b4_copyright([Stack handling
for Bison C
++ parsers
], [2002, 2003, 2004])[
913 #ifndef BISON_STACK_HH
914 # define BISON_STACK_HH
920 template < class T
, class S
= std::deque
< T
> >
925 typedef typename
S::iterator Iterator
;
926 typedef typename
S::const_iterator ConstIterator
;
932 Stack (unsigned int n
) : seq_ (n
)
938 operator [] (unsigned int i
)
945 operator [] (unsigned int i
) const
959 pop (unsigned int n
= 1)
972 inline ConstIterator
begin () const { return seq_
.begin (); }
973 inline ConstIterator
end () const { return seq_
.end (); }
980 template < class T
, class S
= Stack
< T
> >
985 Slice (const S
& stack
,
986 unsigned int range
) : stack_ (stack
),
993 operator [] (unsigned int i
) const
995 return stack_
[range_
- i
];
1001 unsigned int range_
;
1005 #endif // not BISON_STACK_HH]
1008 b4_copyright([Position
class for Bison C
++ parsers
], [2002, 2003, 2004])[
1011 ** \file position.hh
1012 ** Define the Location class.
1015 #ifndef BISON_POSITION_HH
1016 # define BISON_POSITION_HH
1018 # include <iostream>
1023 /** \brief Abstract a Position. */
1027 /** \brief Initial column number. */
1028 static const unsigned int initial_column
= 0;
1029 /** \brief Initial line number. */
1030 static const unsigned int initial_line
= 1;
1032 /** \name Ctor & dtor.
1035 /** \brief Construct a Position. */
1038 line (initial_line
),
1039 column (initial_column
)
1045 /** \name Line and Column related manipulators
1048 /** \brief (line related) Advance to the COUNT next lines. */
1049 inline void lines (int count
= 1)
1051 column
= initial_column
;
1055 /** \brief (column related) Advance to the COUNT next columns. */
1056 inline void columns (int count
= 1)
1058 int leftmost
= initial_column
;
1059 int current
= column
;
1060 if (leftmost
<= current
+ count
)
1063 column
= initial_column
;
1068 /** \brief File name to which this position refers. */
1069 std::string filename
;
1070 /** \brief Current line number. */
1072 /** \brief Current column number. */
1073 unsigned int column
;
1076 /** \brief Add and assign a Position. */
1077 inline const Position
&
1078 operator+= (Position
& res
, const int width
)
1080 res
.columns (width
);
1084 /** \brief Add two Position objects. */
1085 inline const Position
1086 operator+ (const Position
& begin
, const int width
)
1088 Position res
= begin
;
1089 return res
+= width
;
1092 /** \brief Add and assign a Position. */
1093 inline const Position
&
1094 operator-= (Position
& res
, const int width
)
1096 return res
+= -width
;
1099 /** \brief Add two Position objects. */
1100 inline const Position
1101 operator- (const Position
& begin
, const int width
)
1103 return begin
+ -width
;
1106 /** \brief Intercept output stream redirection.
1107 ** \param ostr the destination output stream
1108 ** \param pos a reference to the Position to redirect
1110 inline std::ostream
&
1111 operator<< (std::ostream
& ostr
, const Position
& pos
)
1113 if (!pos
.filename
.empty ())
1114 ostr
<< pos
.filename
<< ':';
1115 return ostr
<< pos
.line
<< '.' << pos
.column
;
1119 #endif // not BISON_POSITION_HH]
1121 b4_copyright([Location
class for Bison C
++ parsers
], [2002, 2003, 2004])[
1124 ** \file location.hh
1125 ** Define the Location class.
1128 #ifndef BISON_LOCATION_HH
1129 # define BISON_LOCATION_HH
1131 # include <iostream>
1133 # include "position.hh"
1138 /** \brief Abstract a Location. */
1141 /** \name Ctor & dtor.
1144 /** \brief Construct a Location. */
1153 /** \name Line and Column related manipulators
1156 /** \brief Reset initial location to final location. */
1157 inline void step (void)
1162 /** \brief Extend the current location to the COUNT next columns. */
1163 inline void columns (unsigned int count
= 1)
1168 /** \brief Extend the current location to the COUNT next lines. */
1169 inline void lines (unsigned int count
= 1)
1177 /** \brief Beginning of the located region. */
1179 /** \brief End of the located region. */
1183 /** \brief Join two Location objects to create a Location. */
1184 inline const Location
operator+ (const Location
& begin
, const Location
& end
)
1186 Location res
= begin
;
1191 /** \brief Add two Location objects */
1192 inline const Location
operator+ (const Location
& begin
, unsigned int width
)
1194 Location res
= begin
;
1195 res
.columns (width
);
1199 /** \brief Add and assign a Location */
1200 inline Location
&operator+= (Location
& res
, unsigned int width
)
1202 res
.columns (width
);
1206 /** \brief Intercept output stream redirection.
1207 ** \param ostr the destination output stream
1208 ** \param loc a reference to the Location to redirect
1210 ** Avoid duplicate information.
1212 inline std::ostream
& operator<< (std::ostream
& ostr
, const Location
& loc
)
1214 Position last
= loc
.end
- 1;
1216 if (loc
.begin
.filename
!= last
.filename
)
1217 ostr
<< '-' << last
;
1218 else if (loc
.begin
.line
!= last
.line
)
1219 ostr
<< '-' << last
.line
<< '.' << last
.column
;
1220 else if (loc
.begin
.column
!= last
.column
)
1221 ostr
<< '-' << last
.column
;
1227 #endif // not BISON_LOCATION_HH]