public ManifestToken PeekNextToken(int it = 1) { int oldIdx = idx; int oldLine = line; int oldColumn = column; ManifestToken oldToken = lastToken; ManifestToken oldToken2 = lastToken2; ManifestToken res = null; for (int i = 0; i < it; i++) { res = GetNextToken(); } idx = oldIdx; line = oldLine; column = oldColumn; lastToken = oldToken; lastToken2 = oldToken2; return(res); }
internal void Parse(ManifestParser parser, ManifestToken cur) { if (parseTabCount > tabCount) { bool isNew = false; if (curChild == null) { isNew = true; curChild = new ManifestNode(); curChild.tabCount = parseTabCount; children.Add(curChild); } curChild.Parse(parser, cur); if (cur.Type == ManifestTokenTypes.EOL) { var next = parser.PeekNextToken(); var next2 = parser.PeekNextToken(2); ManifestTokenTypes nextType = ManifestTokenTypes.EOF; int nextTab = 0; if (next.Type == ManifestTokenTypes.Tabstop) { nextTab = ((ManifestTabStop)next).Count; nextType = next2.Type; } else { nextType = next.Type; parseTabCount = 0; } if ((nextTab <= curChild.tabCount && nextType == ManifestTokenTypes.String) || (nextTab < curChild.tabCount && nextType == ManifestTokenTypes.ValueBegin)) { curChild = null; } } if (isNew) { curChild.tabCount = parseTabCount; } } else if (parseTabCount < tabCount) { curChild = null; return; } else { if (curChild != null) { if (curChild.tabCount > tabCount || (curChild.tabCount == tabCount && cur.Type == ManifestTokenTypes.ValueBegin)) { if (curChild.tabCount == tabCount) { valueBegin = true; } curChild = null; } else { curChild.Parse(parser, cur); } } else { switch (cur.Type) { case ManifestTokenTypes.Tabstop: if (parser.LastToken == null || parser.LastToken.Type == ManifestTokenTypes.EOL) { parseTabCount = ((ManifestTabStop)cur).Count; } break; case ManifestTokenTypes.ValueBegin: if (parser.LastToken == null || parser.LastToken.Type == ManifestTokenTypes.EOL) { valueBegin = true; } else { throw new System.NotSupportedException("Unexpected token:" + cur); } break; case ManifestTokenTypes.String: { //var lastType = parser.LastToken.Type; } break; case ManifestTokenTypes.Node: if (parser.LastToken.Type == ManifestTokenTypes.String) { if (valueBegin) { if (curChild == null) { curChild = new ManifestNode(); curChild.Name = parser.LastToken.Text; curChild.tabCount = tabCount; values.Add(curChild); } else { throw new System.NotSupportedException("Unexpected token:" + cur); } } else if (propertyBegin) { if (pKey == null) { pKey = parser.LastToken.Text; } else { throw new System.NotSupportedException("Unexpected token:" + cur); } } else { Name = parser.LastToken.Text; } } else { throw new System.NotSupportedException("Unexpected token:" + cur); } break; case ManifestTokenTypes.Comma: { if (propertyBegin) { if (pKey != null) { if (parser.LastToken.Type == ManifestTokenTypes.String) { properties[pKey] = parser.LastToken.Text; pKey = null; } else { throw new System.NotSupportedException("Unexpected token:" + cur); } } else { throw new System.NotSupportedException("Unexpected token:" + cur); } } else { throw new System.NotSupportedException("Unexpected token:" + cur); } } break; case ManifestTokenTypes.EOL: case ManifestTokenTypes.EOF: if (!propertyBegin) { if (valueBegin) { if (curChild == null) { curChild = new ManifestNode(); curChild.Name = parser.LastToken.Text; curChild.tabCount = tabCount; values.Add(curChild); } else { throw new System.NotSupportedException("Unexpected token:" + cur); } if (cur.Type == ManifestTokenTypes.EOL) { if (parser.PeekNextToken().Type == ManifestTokenTypes.Tabstop && parser.PeekNextToken(2).Type == ManifestTokenTypes.String) { parser.GetNextToken(); var node = parser.GetNextToken(); curChild.Name += node.Text; } } } else { if (parser.LastToken.Type == ManifestTokenTypes.String) { InnerText = parser.LastToken.Text; } } } else { throw new System.NotSupportedException("Unexpected token:" + cur); } propertyBegin = false; valueBegin = false; break; case ManifestTokenTypes.PropertyBegin: if (!propertyBegin) { propertyBegin = true; } else { throw new System.NotSupportedException("Unexpected token:" + cur); } break; case ManifestTokenTypes.PropertyEnd: if (propertyBegin) { if (pKey != null) { if (parser.LastToken.Type == ManifestTokenTypes.String) { properties[pKey] = parser.LastToken.Text; pKey = null; } else { throw new System.NotSupportedException("Unexpected token:" + cur); } } propertyBegin = false; } break; default: throw new System.NotSupportedException("Unexpected token:" + cur); } } } }
public ManifestToken GetNextToken() { System.Text.StringBuilder sb = new System.Text.StringBuilder(); bool isEscape = false, isFinished = false, stringBlock = false; string escapecode = null; System.Text.StringBuilder sbEscape = null; ManifestToken curToken = null; while (idx < content.Length) { if (curToken != null && isFinished) { curToken.Text = sb.ToString(); lastToken2 = lastToken; lastToken = curToken; return(curToken); } char c = content[idx]; if (curToken == null) { if (c == ' ') { curToken = new ManifestTabStop(); } else if (c == '\n') { curToken = new ManifestEOL(); curToken.LineNumber = line; curToken.ColumnNumber = column; curToken.Text = ""; lastToken2 = lastToken; lastToken = curToken; line++; idx++; column = 1; return(curToken); } else if (c == '\r') { idx++; column++; continue; } else if (c == '{') { curToken = new ManifestPropertyBegin(); } else if (c == '}') { curToken = new ManifestPropertyEnd(); } else if (c == ',') { curToken = new ManifestComma(); } else if (c == ':') { curToken = new ManifestNodeToken(); } else if (c == '-') { if (lastToken == null || lastToken.Type == ManifestTokenTypes.EOL) { curToken = new ManifestValueBegin(); } else { curToken = new ManifestStringToken(); } } else { curToken = new ManifestStringToken(); } curToken.LineNumber = line; curToken.ColumnNumber = column; } switch (curToken.Type) { case ManifestTokenTypes.EOL: case ManifestTokenTypes.PropertyBegin: case ManifestTokenTypes.PropertyEnd: case ManifestTokenTypes.ValueBegin: case ManifestTokenTypes.Comma: case ManifestTokenTypes.EOF: case ManifestTokenTypes.Node: isFinished = true; break; case ManifestTokenTypes.Tabstop: { if (c != ' ') { isFinished = true; ((ManifestTabStop)curToken).Count = sb.Length; continue; } } break; case ManifestTokenTypes.String: { if (IsTerminalChar(c) && !stringBlock) { isFinished = true; if (isEscape) { sb.Append(FinishEscape(escapecode, sbEscape.ToString())); isEscape = false; } continue; } if (isEscape) { if (escapecode == null) { escapecode = c.ToString(); } else { sbEscape.Append(c); if (sbEscape.Length >= GetEscapeLength(escapecode)) { sb.Append(FinishEscape(escapecode, sbEscape.ToString())); isEscape = false; } } idx++; column++; continue; } else { if (c == '\\') { isEscape = true; sbEscape = new System.Text.StringBuilder(); escapecode = null; idx++; column++; continue; } if (c == '\"') { stringBlock = !stringBlock; idx++; column++; continue; } } } break; } idx++; column++; sb.Append(c); } if (idx >= content.Length) { lastToken2 = lastToken; lastToken = new ManifestEOF(); lastToken.LineNumber = line; lastToken.ColumnNumber = column; isFinished = true; return(LastToken); } return(null); }