本文整理汇总了C#中System.ParserState类的典型用法代码示例。如果您正苦于以下问题:C# ParserState类的具体用法?C# ParserState怎么用?C# ParserState使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
ParserState类属于System命名空间,在下文中一共展示了ParserState类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: Parse
public override ParseTree Parse(Lexer lexer, ParserState state)
{
state.RuntimeState.Runtime.ParseTrace.Enter(this, lexer.CurrentSource(), "Pattern " + Type.Name);
int start = lexer.Position;
if (state.Excluded.Contains(this))
{
state.RuntimeState.Runtime.ParseTrace.No(this, lexer.CurrentSource(), "Excluded");
return ParseTree.No;
}
Precedence oldCurrentPrecedence = state.CurrentPrecedence;
if (Precedence.Overwrites(state.CurrentPrecedence))
state.CurrentPrecedence = Precedence;
ParseTree tree = ParseGraph.Parse(lexer, state);
state.CurrentPrecedence = oldCurrentPrecedence;
if (tree == ParseTree.No)
{
state.RuntimeState.Runtime.ParseTrace.No(this, lexer.SourceFrom(start));
return ParseTree.No;
}
state.RuntimeState.Runtime.ParseTrace.Yes(this, lexer.SourceFrom(start));
return tree;
}
示例2: EndBlock
private void EndBlock()
{
AddParameter(parameter);
parameter = new Parameter();
PullParent();
parserState = ParserState.BeginParameterName;
}
示例3: Parse
/// <summary>
/// Parses a stream chunking based on STX/ETX framing. Calls are re-entrant and hold state internally.
/// </summary>
/// <param name="bytes">A byte array of data to append</param>
private void Parse(ArraySegment<byte> bytes)
{
byte[] data = bytes.Array;
for (int i = bytes.Offset; i < bytes.Offset + bytes.Count; i++)
{
if ((data[i] > 3 || data[i] == 1) && _currentState == ParserState.AwaitingEtx)
{
if (_bufferIndex == _messageBuffer.Length)
{
var tmp = new byte[_messageBuffer.Length * 2];
Buffer.BlockCopy(_messageBuffer, 0, tmp, 0, _messageBuffer.Length);
_messageBuffer = tmp;
}
_messageBuffer[_bufferIndex] = data[i];
_bufferIndex++;
}
else if (data[i] == STX)
{
_currentState = ParserState.AwaitingEtx;
_bufferIndex = 0;
}
else if (data[i] == ETX && _currentState == ParserState.AwaitingEtx)
{
_currentState = ParserState.AwaitingStx;
if (_receivedHandler != null)
_receivedHandler(new ArraySegment<byte>(_messageBuffer, 0, _bufferIndex));
_bufferIndex = 0;
}
}
}
示例4: Parse
public override ParseTree Parse(Lexer lexer, ParserState state)
{
lexer.Whitespace(state.RuntimeState);
int start = lexer.Position;
state.RuntimeState.Runtime.ParseTrace.Enter(this, lexer.CurrentSource(), range.ToString());
char character = lexer.Peek();
if (!range.Contains(character))
{
lexer.ErrorStrings[start].Add(range.ToString());
state.RuntimeState.Runtime.ParseTrace.No(this,
lexer.SourceFrom(start), TextEscape.Quote(character));
return ParseTree.No;
}
lexer.Read();
state.RuntimeState.Runtime.ParseTrace.Yes(this,
lexer.SourceFrom(start), TextEscape.Quote(character));
if (state.BuildTextNodes)
return new ParseTree(Convert.ToString(character));
else
return ParseTree.Yes;
}
示例5: Handle
public static void Handle(string timestamp, string data, ParserState state)
{
data = data.Trim();
var match = Regexes.OptionsEntityRegex.Match(data);
if(match.Success)
{
var id = match.Groups[1].Value;
state.Options = new Options {Id = int.Parse(id), OptionList = new List<Option>(), TimeStamp = timestamp};
state.UpdateCurrentNode(typeof(Game));
if(state.Node.Type == typeof(Game))
((Game)state.Node.Object).Data.Add(state.Options);
else
throw new Exception("Invalid node " + state.Node.Type + " -- " + data);
return;
}
match = Regexes.OptionsOptionRegex.Match(data);
if(match.Success)
{
var index = match.Groups[1].Value;
var rawType = match.Groups[2].Value;
var rawEntity = match.Groups[3].Value;
var entity = Helper.ParseEntity(rawEntity, state);
var type = Helper.ParseEnum<OptionType>(rawType);
var option = new Option {Entity = entity, Index = int.Parse(index), Type = type, OptionItems = new List<OptionItem>()};
state.Options.OptionList.Add(option);
state.CurrentOption = option;
state.LastOption = option;
return;
}
match = Regexes.OptionsSuboptionRegex.Match(data);
if(match.Success)
{
var subOptionType = match.Groups[1].Value;
var index = match.Groups[2].Value;
var rawEntity = match.Groups[3].Value;
var entity = Helper.ParseEntity(rawEntity, state);
if(subOptionType == "subOption")
{
var subOption = new SubOption {Entity = entity, Index = int.Parse(index), Targets = new List<Target>()};
state.CurrentOption.OptionItems.Add(subOption);
state.LastOption = subOption;
}
else if(subOptionType == "target")
{
var target = new Target {Entity = entity, Index = int.Parse(index)};
var lastOption = state.LastOption as Option;
if(lastOption != null)
{
lastOption.OptionItems.Add(target);
return;
}
var lastSubOption = state.LastOption as SubOption;
if(lastSubOption != null)
lastSubOption.Targets.Add(target);
}
else
throw new Exception("Unexpected suboption type: " + subOptionType);
}
}
示例6: Handle
public static void Handle(string timestamp, string data, ParserState state)
{
data = data.Trim();
var match = Regexes.EntitiesChosenRegex.Match(data);
if(match.Success)
{
/*NOTE: in 10357, "Player" is bugged, it's treating a player ID
as an entity ID, resulting in "Player=GameEntity"
For our own sanity we keep the old playerID logic from the
previous builds, we'll change to "player" when it's fixed.*/
var rawEntity = match.Groups[1].Value;
var rawPlayer = match.Groups[2].Value;
var count = int.Parse(match.Groups[3].Value);
var entity = Helper.ParseEntity(rawEntity, state);
var player = Helper.ParseEntity(rawPlayer, state);
var cEntities = new ChosenEntities {Entity = entity, PlayerId = player, Count = count, Choices = new List<Choice>(), TimeStamp = timestamp};
state.CurrentGame.Data.Add(cEntities);
state.CurrentChosenEntites = cEntities;
return;
}
match = Regexes.EntitiesChosenEntitiesRegex.Match(data);
if(match.Success)
{
var index = int.Parse(match.Groups[1].Value);
var rawEntity = match.Groups[2].Value;
var entity = Helper.ParseEntity(rawEntity, state);
var choice = new Choice {Entity = entity, Index = index};
state.CurrentChosenEntites.Choices.Add(choice);
return;
}
Console.WriteLine("Warning: Unhandled chosen entities: " + data);
}
示例7: Parse
public override ParseTree Parse(Lexer lexer, ParserState state)
{
lexer.Whitespace(state.RuntimeState);
int start = lexer.Position;
state.RuntimeState.Runtime.ParseTrace.Enter(this, lexer.CurrentSource(), "[]");
char character = lexer.Peek();
if (character == '\0')
{
state.RuntimeState.Runtime.ParseTrace.No(this, lexer.SourceFrom(start), "End of source");
return ParseTree.No;
}
lexer.Read();
state.RuntimeState.Runtime.ParseTrace.Yes(this, lexer.SourceFrom(start), TextEscape.Quote(character));
if (state.BuildTextNodes)
return new ParseTree(Convert.ToString(character));
else
return ParseTree.Yes;
}
示例8: Handle
public static void Handle(string timestamp, string data, ParserState state)
{
data = data.Trim();
var match = Regexes.SendChoicesChoicetypeRegex.Match(data);
if(match.Success)
{
var id = match.Groups[1].Value;
var rawType = match.Groups[2].Value;
var type = Helper.ParseEnum<ChoiceType>(rawType);
state.SendChoices = new SendChoices {Choices = new List<Choice>(), Entity = int.Parse(id), Type = type, TimeStamp = timestamp};
if(state.Node.Type == typeof(Game))
((Game)state.Node.Object).Data.Add(state.SendChoices);
else if(state.Node.Type == typeof(Action))
((Action)state.Node.Object).Data.Add(state.SendChoices);
else
throw new Exception("Invalid node " + state.Node.Type + " -- " + data);
return;
}
match = Regexes.SendChoicesEntitiesRegex.Match(data);
if(match.Success)
{
var index = Helper.ParseEntity(match.Groups[1].Value, state);
var id = Helper.ParseEntity(match.Groups[2].Value, state);
var choice = new Choice {Entity = id, Index = index};
state.SendChoices.Choices.Add(choice);
}
}
示例9: Parse
void Parse()
{
for (var i = 0; i < _data.Length; i++)
{
var c = _data[i];
if (c == '<' && State == ParserState.None)
{
State = ParserState.StartElement;
_elementNameStart = i + 1;
_elementNameEnd = null;
}
else if (State == ParserState.StartElement && CheckPrev(i, CommentStart))
{
State = ParserState.InsideComment;
}
else if (State == ParserState.InsideComment && CheckPrev(i, CommentEnd))
{
State = ParserState.None;
}
else if (State == ParserState.StartElement && CheckPrev(i, CdataStart))
{
State = ParserState.InsideCdata;
}
else if (State == ParserState.InsideCdata && CheckPrev(i, CdataEnd))
{
State = ParserState.None;
}
else if (State == ParserState.StartElement && char.IsWhiteSpace(c))
{
State = ParserState.InsideElement;
_attributeNameStart = i;
_elementNameEnd = i - 1;
}
else if ((State == ParserState.InsideElement || State == ParserState.StartElement) && c == '>')
{
State = ParserState.None;
}
else if (State == ParserState.InsideElement && (char.IsLetter(c) || c=='_' || c==':'))
{
State = ParserState.StartAttribute;
_attributeNameStart = i;
_attributeNameEnd = null;
}
else if (State == ParserState.StartAttribute && (c == '=' || char.IsWhiteSpace(c)))
{
State = ParserState.BeforeAttributeValue;
_attributeNameEnd = i - 1;
}
else if (State == ParserState.BeforeAttributeValue && c == '"')
{
State = ParserState.AttributeValue;
_attributeValueStart = i + 1;
}
else if (State == ParserState.AttributeValue && c == '"')
{
State = ParserState.InsideElement;
}
}
}
示例10: BeginBlock
private void BeginBlock()
{
parameter.Value = new List<Parameter>();
AddParameter(parameter);
PushParent(parameter);
parameter = new Parameter();
parserState = ParserState.BeginParameterName;
}
示例11: TransitionAction
public TransitionAction(ParserAction action, ParserState nextState)
: this(action)
{
if (action != ParserAction.Shift && action != ParserAction.Goto)
throw new Exception("Can only define the next state for the shift or goto actions.");
if (nextState == null)
throw new ArgumentNullException("nextState");
NextState = nextState;
}
示例12: Parse
public override ParseTree Parse(Lexer lexer, ParserState state)
{
int start = lexer.Position;
ParseTree oldLHS = state.LeftHandSide;
bool oldPrecedenceCanEqualCurrent = state.PrecedenceCanEqualCurrent;
ConcretePattern oldRecursionExclude = null;
ParseTree tree = ParseTree.Yes;
for (int n = 0; n < nodes.Count; n++)
{
ParseGraphNode node = nodes[n];
ParseTree nodeTree = node.Parse(lexer, state);
if (nodeTree == ParseTree.No)
{
state.PrecedenceCanEqualCurrent = oldPrecedenceCanEqualCurrent;
state.LeftHandSide = oldLHS;
state.RecursionExclude = oldRecursionExclude;
if (oldRecursionExclude != null)
state.Excluded.Add(oldRecursionExclude);
lexer.Position = start;
return ParseTree.No;
}
tree = tree.Extend(nodeTree);
if (n == 0)
{
if (state.RecursionExclude != null)
{
oldRecursionExclude = state.RecursionExclude;
state.Excluded.Remove(state.RecursionExclude);
state.RecursionExclude = null;
}
if (state.RecursionBehaviour == RecursionBehaviour.RightRecursive)
state.PrecedenceCanEqualCurrent = true;
else if (state.RecursionBehaviour == RecursionBehaviour.LeftRecursive)
state.LeftHandSide = null;
}
}
state.PrecedenceCanEqualCurrent = oldPrecedenceCanEqualCurrent;
state.LeftHandSide = oldLHS;
state.RecursionExclude = oldRecursionExclude;
if (oldRecursionExclude != null)
state.Excluded.Add(oldRecursionExclude);
return tree;
}
示例13: TestParseErrorGetsReported
public void TestParseErrorGetsReported()
{
// Create an instance of our language grammar and set handlers for its rules.
LanguageGrammar<Expression> g = new LanguageGrammar<Expression>();
g.DivideRule.Action = (e1, e2) => Expression.Divide(e1, e2);
g.DoubleLiteralRule.Action = (d) => Expression.Constant(d);
g.IntLiteralRule.Action = (i) => Expression.Constant(i);
g.StringLiteralRule.Action = (s) => Expression.Constant(s);
g.MinusRule.Action = (e1, e2) => Expression.Subtract(e1, e2);
g.ModRule.Action = (e1, e2) => Expression.Modulo(e1, e2);
g.MultiplyRule.Action = (e1, e2) => Expression.Multiply(e1, e2);
g.NegateRule.Action = (e) => Expression.Negate(e);
g.PlusRule.Action = (e1, e2) => Expression.Add(e1, e2);
g.VariableRefRule.SetStatefulAction<ParserState>((s, name) => s.GetVariable(name));
// Spin up a parser for our language.
// TODO: Package this up and simplify it.
var expressionHelper = new ExpressionHelper();
var classifierSession = new TerminalClassifierSession<char, ParserState, int>()
.AddSkipTerminal(new Terminal<char> { Name = "Whitespace", InitialState = RegexCharNFABuilder.RegexCompiler(@"\s+") })
.CurrentCharExprIs(x => x.CurrentChar())
.GetFromMarkExprIs(x => x.GetFromMarkedPos())
.HasCurrentCharExprIs(x => x.HasCurrentChar())
.MarkPosExprIs(x => x.MarkPos())
.MoveNextCharExprIs(x => x.MoveNextChar())
.GetLocationIs(x=>new ParseLocation {Line = 1, Column = x.GetPos()})
.ErrorCollectionIs(x=>x.Errors)
.UnmarkPosExprIs(x => x.UnmarkPos());
var parserGen = new ParserGenerator<char>(expressionHelper);
var parseTableBuilder = new LRParseTableBuilder();
var parseTable = parseTableBuilder.BuildParseTable(g);
var session = parserGen.NewSession<ParserState>()
.NonTerminalValueExprIs<object>(x => x.NonTerminalValue)
.TerminalValueExprIs<string>(x => x.TerminalStringValue)
.TerminalValueExprIs<int>(x => x.TerminalIntValue)
.TerminalValueExprIs<double>(x => x.TerminalDoubleValue)
.TerminalIs(x => x.Terminal)
.NonTerminalIs(x => x.NonTerminal)
.IncludeSymbols(true)
.DebugOutputIs(x => Debug.WriteLine(x))
.Generate("LanguageParser", parseTable, classifierSession);
// At this point, session is an Expression<ParserState,int> representing our parser.
// We can compile it into a delegate or a MethodBuilder. For the examples, we'll use a delegate.
var compiler = session.Compile();
// Create a parser state object and initialize it.
ParserState ps = new ParserState("x*y +* 2.0");
ps.SetParameters(
Expression.Parameter(typeof(double), "x"),
Expression.Parameter(typeof(double), "y"));
Assert.AreNotEqual(0, compiler(ps));
Assert.AreNotEqual(0, ps.Errors.Count);
Assert.Less(ps.Errors.First().ExpectedTerminalNames.Count, g.Terminals.Count);
}
示例14: AddTransition
public void AddTransition(ParserState transitionFrom, GrammarSymbol symbol, ParserState transitionTo)
{
if(!_stateTransitions.ContainsKey(transitionFrom))
_stateTransitions.Add(transitionFrom, new Dictionary<GrammarSymbol,ParserState>());
if (_stateTransitions[transitionFrom].ContainsKey(symbol))
throw new Exception("State conflict. Trying to add a transition for a state when one already exists for the specified grammar symbol.");
_stateTransitions[transitionFrom][symbol] = transitionTo;
_states.Add(transitionFrom);
_states.Add(transitionTo);
}
示例15: RPN_CalculatorEngine
public RPN_CalculatorEngine()
{
curr_state = ParserState.ParserState_Ok;
op_stack = new Stack<CalculatorToken>(32);
output_queue = new Stack<CalculatorToken>(32);
func_table_ = new FunctionTable();
input_string = null;
input_iter = 0;
input_end_marker = 0;
}