3 * Copyright (C) 2008-2009 Florian Brosch, Didier Villevalois
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public
7 * License as published by the Free Software Foundation; either
8 * version 2.1 of the License, or (at your option) any later version.
10 * This library 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 GNU
13 * Lesser General Public License for more details.
15 * You should have received a copy of the GNU Lesser General Public
16 * License along with this library; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20 * Didier 'Ptitjes Villevalois <ptitjes@free.fr>
24 internal class Valadoc
.SequenceRule
: Rule
{
26 public SequenceRule (Object
[] scheme
) {
30 private Object
[] _scheme
;
32 private class State
: Object
{
36 public override bool is_optional () {
40 public override bool starts_with_token (Token token
) {
41 return test_token (0, token
);
44 private bool test_token (int from_index
, Token token
) {
46 while (i
< _scheme
.length
) {
47 if (has_start_token (_scheme
[i
], token
)) {
50 if (!is_optional_rule (_scheme
[i
])) {
58 private bool test_reduce (int from_index
, Token token
) {
60 while (i
< _scheme
.length
) {
61 if (!is_optional_rule (_scheme
[i
])) {
69 public override bool accept_token (Token token
, ParserCallback parser
,
70 Rule
.Forward forward
) throws ParserError
72 var state
= parser
.get_rule_state () as State
;
75 parser
.set_rule_state (state
);
78 if (state
.index
== 0) {
80 } else if (state
.index
== _scheme
.length
) {
85 Object? scheme_element
= null;
88 scheme_element
= _scheme
[state
.index
];
89 if (try_to_apply (scheme_element
, token
, parser
, out handled
)) {
93 if (!is_optional_rule (scheme_element
)) {
96 ((Rule
) scheme_element
).do_skip (parser
);
99 } while (state
.index
< _scheme
.length
);
101 if (state
.index
== _scheme
.length
) {
106 if (scheme_element is TokenType
) {
107 parser
.error (token
, "expected %s".printf (((TokenType
) scheme_element
).to_pretty_string ()));
109 parser
.error (token
, "unexpected token");
111 assert_not_reached ();
114 public override bool would_accept_token (Token token
, Object? rule_state
) {
115 var state
= rule_state as State
;
116 return test_token (state
.index
, token
);
119 public override bool would_reduce (Token token
, Object? rule_state
) {
120 var state
= rule_state as State
;
121 return state
.index
== _scheme
.length
|| test_reduce (state
.index
, token
);
124 public override string to_string (Object? rule_state
) {
125 var state
= rule_state as State
;
127 state
= new
State ();
129 return "%-15s%-15s(index=%d/%d)".printf (name
!= null ? name
: " ", "[seq]", state
.index
, _scheme
.length
);