RusticToken CaptureValueOrLeftOperator(string expression, ref int index) { RusticToken result = TryCaptureValueOrLeftOperator(expression, ref index, canEndGroups: false); if (result == null) { throw new Exception("Unexpected token: " + unexpectedTokenExpr.Match(expression, index)); } return(result); }
public void PutToken(RusticToken token) { switch (token.mode) { case RusticTokenMode.Literal: PutValueToken(token.value); break; case RusticTokenMode.VariableName: PutVariableToken(token.value.ToString()); break; case RusticTokenMode.Operation: PutOperationToken((RusticOperation)token.value); break; case RusticTokenMode.PriorityOffset: ChangePriority((int)token.value); break; default: throw new NotImplementedException(); } }
public IEnumerable <RusticToken> GetTokenList(string expression) { if (initialized == false) { Initialize(); } int index = 0; expression.Trim(); currentState.Clear(); currentState.Push(ParsingState.ValueOrLeftOperatorOrEnd); while (currentState.Count > 0) { TryCaptureIgnoredPattern(expression, ref index); RusticToken result = null; switch (currentState.Pop()) { case ParsingState.ValueOrLeftOperatorOrEnd: result = TryCaptureValueOrLeftOperator(expression, ref index); break; case ParsingState.ValueOrLeftOperatorExpected: result = CaptureValueOrLeftOperator(expression, ref index); break; case ParsingState.MiddleOperatorOrEnd: result = TryCaptureMiddleOperator(expression, ref index); break; } if (result != null) { yield return(result); } } TryCaptureIgnoredPattern(expression, ref index); if (index < expression.Length) { throw new Exception($"Unexpected sequence of characters: {expression.Substring(index)}"); } }