1 /*--------------------------------------------------------------------------
\r
2 * Copyright 2009 Taro L. Saito
\r
4 * Licensed under the Apache License, Version 2.0 (the "License");
\r
5 * you may not use this file except in compliance with the License.
\r
6 * You may obtain a copy of the License at
\r
8 * http://www.apache.org/licenses/LICENSE-2.0
\r
10 * Unless required by applicable law or agreed to in writing, software
\r
11 * distributed under the License is distributed on an "AS IS" BASIS,
\r
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
\r
13 * See the License for the specific language governing permissions and
\r
14 * limitations under the License.
\r
15 *--------------------------------------------------------------------------*/
\r
16 //--------------------------------------
\r
19 // SilkLexerState.java
\r
20 // Since: 2009/01/29 20:12:51
\r
24 //--------------------------------------
\r
25 package org.xerial.silk.impl;
\r
27 import org.xerial.util.ArrayDeque;
\r
28 import org.xerial.util.Deque;
\r
29 import org.xerial.util.graph.Automaton;
\r
30 import org.xerial.util.graph.AutomatonCursor;
\r
36 * DEFAULT -(NodeStart)-> KEY
\r
37 * KEY -> (Colon) -> OUT
\r
38 * OUT -> (LParen) -> IN
\r
39 * IN -> (Colon) -> IN
\r
41 * ($current = ANY) -> (Other) -> ($current)
\r
47 public class SilkLexerState
\r
49 public static enum State {
\r
50 INIT, OUT_KEY, OUT_VALUE, IN_VALUE, IN_KEY, IN_FUNC_NAME
\r
53 public static enum Symbol {
\r
54 NodeStart, Colon, EnterParen, LeaveParen, LeaveValue, At
\r
57 private static final Automaton<State, Symbol> automaton = new Automaton<State, Symbol>();
\r
58 private Deque<State> stateStack = new ArrayDeque<State>();
\r
60 //private State beforeJSONState = State.INIT;
\r
61 //private int nestLevel = 0;
\r
62 private final AutomatonCursor<State, Symbol> cursor;
\r
66 automaton.addTransition(State.INIT, Symbol.NodeStart, State.OUT_KEY);
\r
67 automaton.addTransition(State.OUT_KEY, Symbol.EnterParen, State.IN_KEY);
\r
68 automaton.addTransition(State.OUT_KEY, Symbol.Colon, State.OUT_VALUE);
\r
69 //automaton.addTransition(State.OUT_VALUE, Symbol.EnterParen, State.IN_KEY);
\r
70 automaton.addTransition(State.IN_KEY, Symbol.Colon, State.IN_VALUE);
\r
71 automaton.addTransition(State.IN_VALUE, Symbol.LeaveValue, State.IN_KEY);
\r
72 automaton.addTransition(State.IN_VALUE, Symbol.EnterParen, State.IN_KEY);
\r
74 automaton.addTransition(State.OUT_VALUE, Symbol.At, State.OUT_KEY);
\r
76 for (State each : State.values())
\r
77 automaton.addStarTransition(each, each);
\r
80 public SilkLexerState()
\r
82 cursor = automaton.cursor(State.INIT);
\r
85 public State transit(Symbol input)
\r
87 State current = getCurrentState();
\r
91 stateStack.addLast(current);
\r
94 if (current != State.OUT_VALUE)
\r
95 stateStack.addLast(current);
\r
98 if (current != State.OUT_VALUE)
\r
100 if (!stateStack.isEmpty())
\r
102 State prevState = stateStack.removeLast();
\r
103 cursor.reset(prevState);
\r
110 return cursor.transit(input);
\r
113 public State getCurrentState()
\r
115 return cursor.getState();
\r
118 public boolean isInKey()
\r
120 State current = cursor.getState();
\r
121 return current == State.IN_KEY || current == State.OUT_KEY;
\r
124 public boolean isValue()
\r
126 State current = cursor.getState();
\r
127 return current == State.IN_VALUE || current == State.OUT_VALUE;
\r
130 public boolean isInValue()
\r
132 return getCurrentState() == State.IN_VALUE;
\r
135 public boolean isOutValue()
\r
137 return getCurrentState() == State.OUT_VALUE;
\r
140 public void reset()
\r
142 cursor.reset(State.INIT);
\r
143 stateStack.clear();
\r
144 // beforeJSONState = State.INIT;
\r