libvaladoc: Replace void* with corresponding Vala API
[vala-gnome.git] / libvaladoc / parser / sequencerule.vala
blobe1e6797a48e1080ede64cc03d43a7b0e8ba74a21
1 /* sequencerule.vala
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
19 * Author:
20 * Didier 'Ptitjes Villevalois <ptitjes@free.fr>
24 internal class Valadoc.SequenceRule : Rule {
26 public SequenceRule (Object[] scheme) {
27 _scheme = scheme;
30 private Object[] _scheme;
32 private class State : Object {
33 public int index = 0;
36 public override bool is_optional () {
37 return false;
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) {
45 int i = from_index;
46 while (i < _scheme.length) {
47 if (has_start_token (_scheme[i], token)) {
48 return true;
50 if (!is_optional_rule (_scheme[i])) {
51 break;
53 i++;
55 return false;
58 private bool test_reduce (int from_index, Token token) {
59 int i = from_index;
60 while (i < _scheme.length) {
61 if (!is_optional_rule (_scheme[i])) {
62 return false;
64 i++;
66 return true;
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;
73 if (state == null) {
74 state = new State ();
75 parser.set_rule_state (state);
78 if (state.index == 0) {
79 do_start (parser);
80 } else if (state.index == _scheme.length) {
81 do_reduce (parser);
82 return false;
85 Object? scheme_element = null;
86 bool handled;
87 do {
88 scheme_element = _scheme[state.index];
89 if (try_to_apply (scheme_element, token, parser, out handled)) {
90 state.index++;
91 return handled;
93 if (!is_optional_rule (scheme_element)) {
94 break;
95 } else {
96 ((Rule) scheme_element).do_skip (parser);
98 state.index++;
99 } while (state.index < _scheme.length);
101 if (state.index == _scheme.length) {
102 do_reduce (parser);
103 return false;
106 if (scheme_element is TokenType) {
107 parser.error (token, "expected %s".printf (((TokenType) scheme_element).to_pretty_string ()));
108 } else {
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;
126 if (state == null) {
127 state = new State ();
129 return "%-15s%-15s(index=%d/%d)".printf (name != null ? name : " ", "[seq]", state.index, _scheme.length);