Commit 91aaa346 authored by thebusytypist's avatar thebusytypist

Finish the new implementation of state machine. But not been unittested.

parent 6b0df217
...@@ -762,14 +762,16 @@ private: ...@@ -762,14 +762,16 @@ private:
// Object states // Object states
IterativeParsingObjectInitialState, IterativeParsingObjectInitialState,
IterativeParsingMemberState, IterativeParsingMemberKeyState,
IterativeParsingObjectDelimiterState, IterativeParsingKeyValueDelimiterState,
IterativeParsingMemberValueState,
IterativeParsingMemberDelimiterState,
IterativeParsingObjectFinishState, IterativeParsingObjectFinishState,
// Array states // Array states
IterativeParsingArrayInitialState, IterativeParsingArrayInitialState,
IterativeParsingElementState, IterativeParsingElementState,
IterativeParsingArrayDelimiterState, IterativeParsingElementDelimiterState,
IterativeParsingArrayFinishState, IterativeParsingArrayFinishState,
cIterativeParsingStateCount cIterativeParsingStateCount
...@@ -784,9 +786,9 @@ private: ...@@ -784,9 +786,9 @@ private:
IterativeParsingRightCurlyBracketToken, IterativeParsingRightCurlyBracketToken,
IterativeParsingCommaToken, IterativeParsingCommaToken,
IterativeParsingColonToken,
IterativeParsingQuotesToken, IterativeParsingStringToken,
IterativeParsingFalseToken, IterativeParsingFalseToken,
IterativeParsingTrueToken, IterativeParsingTrueToken,
IterativeParsingNullToken, IterativeParsingNullToken,
...@@ -802,7 +804,8 @@ private: ...@@ -802,7 +804,8 @@ private:
case '{': return IterativeParsingLeftCurlyBracketToken; case '{': return IterativeParsingLeftCurlyBracketToken;
case '}': return IterativeParsingRightCurlyBracketToken; case '}': return IterativeParsingRightCurlyBracketToken;
case ',': return IterativeParsingCommaToken; case ',': return IterativeParsingCommaToken;
case '"': return IterativeParsingQuotesToken; case ':': return IterativeParsingColonToken;
case '"': return IterativeParsingStringToken;
case 'f': return IterativeParsingFalseToken; case 'f': return IterativeParsingFalseToken;
case 't': return IterativeParsingTrueToken; case 't': return IterativeParsingTrueToken;
case 'n': return IterativeParsingNullToken; case 'n': return IterativeParsingNullToken;
...@@ -815,12 +818,13 @@ private: ...@@ -815,12 +818,13 @@ private:
static const IterativeParsingState G[cIterativeParsingStateCount][cIterativeParsingTokenCount] = { static const IterativeParsingState G[cIterativeParsingStateCount][cIterativeParsingTokenCount] = {
// Start // Start
{ {
IterativeParsingObjectInitialState, // Left bracket IterativeParsingArrayInitialState, // Left bracket
IterativeParsingErrorState, // Right bracket IterativeParsingErrorState, // Right bracket
IterativeParsingArrayInitialState, // Left curly bracket IterativeParsingObjectInitialState, // Left curly bracket
IterativeParsingErrorState, // Right curly bracket IterativeParsingErrorState, // Right curly bracket
IterativeParsingErrorState, // Comma IterativeParsingErrorState, // Comma
IterativeParsingErrorState, // Quotes IterativeParsingErrorState, // Colon
IterativeParsingErrorState, // String
IterativeParsingErrorState, // False IterativeParsingErrorState, // False
IterativeParsingErrorState, // True IterativeParsingErrorState, // True
IterativeParsingErrorState, // Null IterativeParsingErrorState, // Null
...@@ -829,12 +833,14 @@ private: ...@@ -829,12 +833,14 @@ private:
// Finish(sink state) // Finish(sink state)
{ {
IterativeParsingErrorState, IterativeParsingErrorState, IterativeParsingErrorState, IterativeParsingErrorState, IterativeParsingErrorState, IterativeParsingErrorState, IterativeParsingErrorState, IterativeParsingErrorState, IterativeParsingErrorState, IterativeParsingErrorState,
IterativeParsingErrorState, IterativeParsingErrorState, IterativeParsingErrorState, IterativeParsingErrorState, IterativeParsingErrorState IterativeParsingErrorState, IterativeParsingErrorState, IterativeParsingErrorState, IterativeParsingErrorState, IterativeParsingErrorState,
IterativeParsingErrorState
}, },
// Error(sink state) // Error(sink state)
{ {
IterativeParsingErrorState, IterativeParsingErrorState, IterativeParsingErrorState, IterativeParsingErrorState, IterativeParsingErrorState, IterativeParsingErrorState, IterativeParsingErrorState, IterativeParsingErrorState, IterativeParsingErrorState, IterativeParsingErrorState,
IterativeParsingErrorState, IterativeParsingErrorState, IterativeParsingErrorState, IterativeParsingErrorState, IterativeParsingErrorState IterativeParsingErrorState, IterativeParsingErrorState, IterativeParsingErrorState, IterativeParsingErrorState, IterativeParsingErrorState,
IterativeParsingErrorState
}, },
// ObjectInitial // ObjectInitial
{ {
...@@ -843,33 +849,64 @@ private: ...@@ -843,33 +849,64 @@ private:
IterativeParsingErrorState, // Left curly bracket IterativeParsingErrorState, // Left curly bracket
IterativeParsingObjectFinishState, // Right curly bracket IterativeParsingObjectFinishState, // Right curly bracket
IterativeParsingErrorState, // Comma IterativeParsingErrorState, // Comma
IterativeParsingMemberState, // Quotes IterativeParsingErrorState, // Colon
IterativeParsingMemberKeyState, // String
IterativeParsingErrorState, // False IterativeParsingErrorState, // False
IterativeParsingErrorState, // True IterativeParsingErrorState, // True
IterativeParsingErrorState, // Null IterativeParsingErrorState, // Null
IterativeParsingErrorState // Number IterativeParsingErrorState // Number
}, },
// Member // MemberKey
{
IterativeParsingErrorState, // Left bracket
IterativeParsingErrorState, // Right bracket
IterativeParsingErrorState, // Left curly bracket
IterativeParsingErrorState, // Right curly bracket
IterativeParsingErrorState, // Comma
IterativeParsingKeyValueDelimiterState, // Colon
IterativeParsingErrorState, // String
IterativeParsingErrorState, // False
IterativeParsingErrorState, // True
IterativeParsingErrorState, // Null
IterativeParsingErrorState // Number
},
// KeyValueDelimiter
{
IterativeParsingArrayInitialState, // Left bracket(push MemberValue state)
IterativeParsingErrorState, // Right bracket
IterativeParsingObjectInitialState, // Left curly bracket(push MemberValue state)
IterativeParsingErrorState, // Right curly bracket
IterativeParsingErrorState, // Comma
IterativeParsingErrorState, // Colon
IterativeParsingMemberValueState, // String
IterativeParsingMemberValueState, // False
IterativeParsingMemberValueState, // True
IterativeParsingMemberValueState, // Null
IterativeParsingMemberValueState // Number
},
// MemberValue
{ {
IterativeParsingErrorState, // Left bracket IterativeParsingErrorState, // Left bracket
IterativeParsingErrorState, // Right bracket IterativeParsingErrorState, // Right bracket
IterativeParsingErrorState, // Left curly bracket IterativeParsingErrorState, // Left curly bracket
IterativeParsingObjectFinishState, // Right curly bracket IterativeParsingObjectFinishState, // Right curly bracket
IterativeParsingObjectDelimiterState, // Comma IterativeParsingMemberDelimiterState, // Comma
IterativeParsingErrorState, // Quotes IterativeParsingErrorState, // Colon
IterativeParsingErrorState, // String
IterativeParsingErrorState, // False IterativeParsingErrorState, // False
IterativeParsingErrorState, // True IterativeParsingErrorState, // True
IterativeParsingErrorState, // Null IterativeParsingErrorState, // Null
IterativeParsingErrorState // Number IterativeParsingErrorState // Number
}, },
// ObjectDelimiter // MemberDelimiter
{ {
IterativeParsingErrorState, // Left bracket IterativeParsingErrorState, // Left bracket
IterativeParsingErrorState, // Right bracket IterativeParsingErrorState, // Right bracket
IterativeParsingErrorState, // Left curly bracket IterativeParsingErrorState, // Left curly bracket
IterativeParsingErrorState, // Right curly bracket IterativeParsingErrorState, // Right curly bracket
IterativeParsingErrorState, // Comma IterativeParsingErrorState, // Comma
IterativeParsingMemberState, // Quotes IterativeParsingErrorState, // Colon
IterativeParsingMemberKeyState, // String
IterativeParsingErrorState, // False IterativeParsingErrorState, // False
IterativeParsingErrorState, // True IterativeParsingErrorState, // True
IterativeParsingErrorState, // Null IterativeParsingErrorState, // Null
...@@ -878,16 +915,18 @@ private: ...@@ -878,16 +915,18 @@ private:
// ObjectFinish(sink state) // ObjectFinish(sink state)
{ {
IterativeParsingErrorState, IterativeParsingErrorState, IterativeParsingErrorState, IterativeParsingErrorState, IterativeParsingErrorState, IterativeParsingErrorState, IterativeParsingErrorState, IterativeParsingErrorState, IterativeParsingErrorState, IterativeParsingErrorState,
IterativeParsingErrorState, IterativeParsingErrorState, IterativeParsingErrorState, IterativeParsingErrorState, IterativeParsingErrorState IterativeParsingErrorState, IterativeParsingErrorState, IterativeParsingErrorState, IterativeParsingErrorState, IterativeParsingErrorState,
IterativeParsingErrorState
}, },
// ArrayInitial // ArrayInitial
{ {
IterativeParsingElementState, // Left bracket IterativeParsingArrayInitialState, // Left bracket(push Element state)
IterativeParsingArrayFinishState, // Right bracket IterativeParsingArrayFinishState, // Right bracket
IterativeParsingElementState, // Left curly bracket IterativeParsingObjectInitialState, // Left curly bracket(push Element state)
IterativeParsingErrorState, // Right curly bracket IterativeParsingErrorState, // Right curly bracket
IterativeParsingErrorState, // Comma IterativeParsingErrorState, // Comma
IterativeParsingElementState, // Quotes IterativeParsingErrorState, // Colon
IterativeParsingElementState, // String
IterativeParsingElementState, // False IterativeParsingElementState, // False
IterativeParsingElementState, // True IterativeParsingElementState, // True
IterativeParsingElementState, // Null IterativeParsingElementState, // Null
...@@ -899,21 +938,23 @@ private: ...@@ -899,21 +938,23 @@ private:
IterativeParsingArrayFinishState, // Right bracket IterativeParsingArrayFinishState, // Right bracket
IterativeParsingErrorState, // Left curly bracket IterativeParsingErrorState, // Left curly bracket
IterativeParsingErrorState, // Right curly bracket IterativeParsingErrorState, // Right curly bracket
IterativeParsingArrayDelimiterState, // Comma IterativeParsingElementDelimiterState, // Comma
IterativeParsingErrorState, // Quotes IterativeParsingErrorState, // Colon
IterativeParsingErrorState, // String
IterativeParsingErrorState, // False IterativeParsingErrorState, // False
IterativeParsingErrorState, // True IterativeParsingErrorState, // True
IterativeParsingErrorState, // Null IterativeParsingErrorState, // Null
IterativeParsingErrorState // Number IterativeParsingErrorState // Number
}, },
// ArrayDelimiter // ElementDelimiter
{ {
IterativeParsingElementState, // Left bracket IterativeParsingArrayInitialState, // Left bracket(push Element state)
IterativeParsingErrorState, // Right bracket IterativeParsingErrorState, // Right bracket
IterativeParsingElementState, // Left curly bracket IterativeParsingObjectInitialState, // Left curly bracket(push Element state)
IterativeParsingErrorState, // Right curly bracket IterativeParsingErrorState, // Right curly bracket
IterativeParsingErrorState, // Comma IterativeParsingErrorState, // Comma
IterativeParsingElementState, // Quotes IterativeParsingErrorState, // Colon
IterativeParsingElementState, // String
IterativeParsingElementState, // False IterativeParsingElementState, // False
IterativeParsingElementState, // True IterativeParsingElementState, // True
IterativeParsingElementState, // Null IterativeParsingElementState, // Null
...@@ -922,15 +963,16 @@ private: ...@@ -922,15 +963,16 @@ private:
// ArrayFinish(sink state) // ArrayFinish(sink state)
{ {
IterativeParsingErrorState, IterativeParsingErrorState, IterativeParsingErrorState, IterativeParsingErrorState, IterativeParsingErrorState, IterativeParsingErrorState, IterativeParsingErrorState, IterativeParsingErrorState, IterativeParsingErrorState, IterativeParsingErrorState,
IterativeParsingErrorState, IterativeParsingErrorState, IterativeParsingErrorState, IterativeParsingErrorState, IterativeParsingErrorState IterativeParsingErrorState, IterativeParsingErrorState, IterativeParsingErrorState, IterativeParsingErrorState, IterativeParsingErrorState,
IterativeParsingErrorState
} }
}; // End of G }; // End of G
return G[state][token]; return G[state][token];
} }
// Store or process informations during state transition. // Make an advance in the token stream and state based on the candidate destination state which was returned by Transit().
// May return a new state. // May return a new state on state pop.
template <unsigned parseFlags, typename InputStream, typename Handler> template <unsigned parseFlags, typename InputStream, typename Handler>
IterativeParsingState Advance(IterativeParsingState src, IterativeParsingToken token, IterativeParsingState dst, InputStream& is, Handler& handler) { IterativeParsingState Advance(IterativeParsingState src, IterativeParsingToken token, IterativeParsingState dst, InputStream& is, Handler& handler) {
int c = 0; int c = 0;
...@@ -951,25 +993,57 @@ private: ...@@ -951,25 +993,57 @@ private:
case IterativeParsingObjectInitialState: case IterativeParsingObjectInitialState:
case IterativeParsingArrayInitialState: case IterativeParsingArrayInitialState:
is.Take(); is.Take();
// Push the state(Element or MemeberValue) if we are nested in another array or value of member.
// In this way we can get the correct state on ObjectFinish or ArrayFinish by frame pop.
n = src;
if (src == IterativeParsingArrayInitialState || src == IterativeParsingElementDelimiterState)
n = IterativeParsingElementState;
else if (src == IterativeParsingKeyValueDelimiterState)
n = IterativeParsingMemberValueState;
// Push current state. // Push current state.
*stack_.template Push<IterativeParsingState>(1) = src; *stack_.template Push<IterativeParsingState>(1) = n;
// Initialize and push the member/element count. // Initialize and push the member/element count.
*stack_.template Push<int>(1) = 0; *stack_.template Push<int>(1) = 0;
// Call handler
if (dst == IterativeParsingObjectInitialState)
handler.StartObject();
else
handler.StartArray();
return dst; return dst;
case IterativeParsingMemberState: case IterativeParsingMemberKeyState:
return dst; ParseString<parseFlags>(is, handler);
if (HasParseError())
return IterativeParsingErrorState;
else
return dst;
case IterativeParsingElementState: case IterativeParsingKeyValueDelimiterState:
if (token == IterativeParsingLeftBracketToken || token == IterativeParsingLeftCurlyBracketToken) { if (token == IterativeParsingColonToken) {
is.Take();
return dst; return dst;
} }
else { else
return IterativeParsingErrorState;
case IterativeParsingMemberValueState:
// Must be non-compound value. Or it would be ObjectInitial or ArrayInitial state.
ParseValue<parseFlags>(is, handler);
if (HasParseError()) {
return IterativeParsingErrorState;
} }
return dst;
case IterativeParsingElementState:
// Must be non-compound value. Or it would be ObjectInitial or ArrayInitial state.
ParseValue<parseFlags>(is, handler);
if (HasParseError()) {
return IterativeParsingErrorState;
}
return dst;
case IterativeParsingObjectDelimiterState: case IterativeParsingMemberDelimiterState:
case IterativeParsingArrayDelimiterState: case IterativeParsingElementDelimiterState:
is.Take(); is.Take();
// Update member/element count. // Update member/element count.
*stack_.template Top<int>() = *stack_.template Top<int>() + 1; *stack_.template Top<int>() = *stack_.template Top<int>() + 1;
...@@ -979,7 +1053,8 @@ private: ...@@ -979,7 +1053,8 @@ private:
is.Take(); is.Take();
// Get member count. // Get member count.
c = *stack_.template Pop<int>(1); c = *stack_.template Pop<int>(1);
if (src == IterativeParsingMemberState) // If the object is not empty, count the last member.
if (src == IterativeParsingMemberValueState)
++c; ++c;
// Restore the state. // Restore the state.
n = *stack_.template Pop<IterativeParsingState>(1); n = *stack_.template Pop<IterativeParsingState>(1);
...@@ -994,6 +1069,7 @@ private: ...@@ -994,6 +1069,7 @@ private:
is.Take(); is.Take();
// Get element count. // Get element count.
c = *stack_.template Pop<int>(1); c = *stack_.template Pop<int>(1);
// If the array is not empty, count the last element.
if (src == IterativeParsingElementState) if (src == IterativeParsingElementState)
++c; ++c;
// Restore the state. // Restore the state.
...@@ -1016,11 +1092,11 @@ private: ...@@ -1016,11 +1092,11 @@ private:
IterativeParsingState state = IterativeParsingStartState; IterativeParsingState state = IterativeParsingStartState;
SkipWhitespace(is); SkipWhitespace(is);
while (is.Peek() != '\0' && state != IterativeParsingErrorState) { while (is.Peek() != '\0') {
IterativeParsingToken t = GuessToken(is.Peek()); IterativeParsingToken t = GuessToken(is.Peek());
IterativeParsingState n = Transit(state, t); IterativeParsingState n = Transit(state, t);
if (Advance<parseFlags>(state, t, n, is, handler)) if ((n = Advance<parseFlags>(state, t, n, is, handler)) != IterativeParsingErrorState)
state = n; state = n;
else else
break; break;
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment