protected bool canProduceValue(Object token) { if (token is SyntaxNode) { return(true); } else if (token is Lexeme) { Lexeme l = token as Lexeme; Lexeme.LexemeType t = l.Type; return(t == Lexeme.LexemeType.REAL_VALUE || properties.IsConstant(l.Value) || properties.IsVariable(l.Value)); } else { if (token is FractionToken || token is FunctionApplyToken || token is ParenthesesToken || token is SuperscriptToken || token is RadicalToken) { return(true); } //else if (token is SubscriptToken) //{ // return isSubscriptedIdentifier(token as SubscriptToken); //} } return(false); }
private void ParseLine(string s, int start) { for (int i = start; i < s.Length;) { //We see a letter character char c = s[i]; if (char.IsLetter(s[i])) { string word = ParseWord(s, i, out i); Lexeme.LexemeType type = Lexeme.LexemeType.identifier; if (isKeyWord(word)) { type = Lexeme.LexemeType.keyword; } _output.Add(new Lexeme(word, type)); } //We see a number character else if (char.IsNumber(s[i])) { string num = ParseNumber(s, i, out i); _output.Add(new Lexeme(num, Lexeme.LexemeType.integerConstant)); } //We see a quote else if (s[i] == '"') { string word = ParseStringConstant(s, i, out i); _output.Add(new Lexeme(word, Lexeme.LexemeType.stringConstant)); } else { string symbol = ParseSymbol(s, i, out i); if (!string.IsNullOrEmpty(symbol)) { _output.Add(new Lexeme(symbol, Lexeme.LexemeType.symbol)); } } } }
//private bool isSubscriptedIdentifier(SubscriptToken t) //{ // TokenList subBase = (token as SubscriptToken).Base; // TokenList subscript = (token as SubscriptToken).Subscript; // if (subBase.Count == 1 && subBase[0] is TextRunToken) // { // String subBaseText = (subBase[0] as TextRunToken).Text; // if (Regex.Match(subBaseText, ) // } // // TODO : implementiraj!!!! // return false; //} protected bool canProcessTokenAsUnaryOp() { if (lastProcessedElement == null) { return(true); } else if (lastProcessedElement is Lexeme) { Lexeme previous = lastProcessedElement as Lexeme; Lexeme.LexemeType type = previous.Type; return(type == Lexeme.LexemeType.LEFT_PAREN || type == Lexeme.LexemeType.EQ_SIGN || type == Lexeme.LexemeType.OP_DIV || type == Lexeme.LexemeType.OP_MUL || type == Lexeme.LexemeType.OP_MINUS || type == Lexeme.LexemeType.OP_PLUS || type == Lexeme.LexemeType.ARGUMENT_SEPARATOR); } else { return(false); } }
protected SyntaxNode processValueProducerLexeme(Lexeme lexeme) { Lexeme.LexemeType lt = lexeme.Type; if (lt == Lexeme.LexemeType.REAL_VALUE) { double value; if (!double.TryParse(lexeme.Value, NumberStyles.Any, CultureInfo.InvariantCulture, out value)) { throw new ParseException("Couldn't parse literal value: " + lexeme.Value); } return(new LiteralNode(value)); } else if (properties.IsVariable(lexeme.Value)) { return(new VariableIdentifierNode(lexeme.Value)); } else if (properties.IsConstant(lexeme.Value)) { Double value = properties.getConstantValue(lexeme.Value); return(new ConstantIdentifierNode(lexeme.Value, value)); } throw new ParseException("Cannot process lexeme " + lexeme.simpleRepresentation() + "as a value producer."); }
protected SyntaxNode buildSyntaxTree(List <ISyntaxUnit> postfixForm) { Queue <ISyntaxUnit> inputQueue = new Queue <ISyntaxUnit>(postfixForm); Stack <SyntaxNode> operandStack = new Stack <SyntaxNode>(); while (inputQueue.Count > 0) { ISyntaxUnit input = inputQueue.Dequeue(); if (input is Lexeme) { Lexeme token = input as Lexeme; Lexeme.LexemeType ttype = token.Type; if (properties.IsVariable(token.Value)) { VariableIdentifierNode variable = new VariableIdentifierNode(token.Value); operandStack.Push(variable); } else if (properties.IsConstant(token.Value)) { double constantValue = properties.getConstantValue(token.Value); ConstantIdentifierNode constant = new ConstantIdentifierNode(token.Value, constantValue); operandStack.Push(constant); } else if (properties.IsFunctionName(token.Value)) { int nArguments = properties.getFunctionArgumentsCount(token.Value); FunctionApplyNode.FunctionBody funcBody = properties.getFunctionDefinition(token.Value); ArgumentListNode argumentList = new ArgumentListNode(); try { for (int i = 0; i < nArguments; i++) { argumentList.addArgument(operandStack.Pop()); } } catch (InvalidOperationException ex) { throw new ParseException("Not enough operands on operand stack for function call."); } FunctionApplyNode functionCall = new FunctionApplyNode(argumentList, funcBody, token.Value); operandStack.Push(functionCall); } else if (ttype == Lexeme.LexemeType.REAL_VALUE) { double value; if (!double.TryParse(token.Value, NumberStyles.Any, CultureInfo.InvariantCulture, out value)) { throw new ParseException("Couldn't parse literal value: " + token.Value); } LiteralNode literal = new LiteralNode(value); operandStack.Push(literal); } else if (ttype == Lexeme.LexemeType.OP_PLUS) { try { SyntaxNode right = operandStack.Pop(); SyntaxNode left = operandStack.Pop(); AdditionNode addition = new AdditionNode(left, right); operandStack.Push(addition); } catch (InvalidOperationException ex) { throw new ParseException("Missing operand(s) for addition."); } } else if (ttype == Lexeme.LexemeType.OP_MINUS) { try { SyntaxNode right = operandStack.Pop(); SyntaxNode left = operandStack.Pop(); SubtractionNode subtraction = new SubtractionNode(left, right); operandStack.Push(subtraction); } catch (InvalidOperationException ex) { throw new ParseException("Missing operand(s) for subtraction."); } } else if (ttype == Lexeme.LexemeType.OP_MUL) { try { SyntaxNode right = operandStack.Pop(); SyntaxNode left = operandStack.Pop(); MultiplicationNode multiplication = new MultiplicationNode(left, right); operandStack.Push(multiplication); } catch (InvalidOperationException ex) { throw new ParseException("Missing operand(s) for multiplication."); } } else if (ttype == Lexeme.LexemeType.OP_DIV) { try { SyntaxNode right = operandStack.Pop(); SyntaxNode left = operandStack.Pop(); DivisionNode division = new DivisionNode(left, right); operandStack.Push(division); } catch (InvalidOperationException ex) { throw new ParseException("Missing operand(s) for division."); } } else if (ttype == Lexeme.LexemeType.OP_POW) { try { SyntaxNode exponent = operandStack.Pop(); SyntaxNode baseNode = operandStack.Pop(); PowerNode power = new PowerNode(baseNode, exponent); operandStack.Push(power); } catch (InvalidOperationException ex) { throw new ParseException("Missing operand(s) for exponentiation."); } } else if (ttype == Lexeme.LexemeType.EQ_SIGN) { try { SyntaxNode right = operandStack.Pop(); SyntaxNode left = operandStack.Pop(); EqualsNode eqNode = new EqualsNode(left, right); operandStack.Push(eqNode); } catch (InvalidOperationException ex) { throw new ParseException("Missing operand(s) for assignment."); } } else if (ttype == Lexeme.LexemeType.OP_PLUS_UNARY) { try { SyntaxNode child = operandStack.Pop(); UnaryPlusNode unaryPlus = new UnaryPlusNode(child); operandStack.Push(unaryPlus); } catch (InvalidOperationException ex) { throw new ParseException("Missing operand for unary plus."); } } else if (ttype == Lexeme.LexemeType.OP_MINUS_UNARY) { try { SyntaxNode child = operandStack.Pop(); UnaryMinusNode unaryMinus = new UnaryMinusNode(child); operandStack.Push(unaryMinus); } catch (InvalidOperationException ex) { throw new ParseException("Missing operand for unary minus."); } } else { throw new ParseException("Unexpected token in postfix expression: " + token.simpleRepresentation()); } } else if (input is SyntaxNode) { operandStack.Push(input as SyntaxNode); } else { throw new ParseException("Unexpected object type in postfix expression."); } } if (operandStack.Count == 1) { return(operandStack.Pop()); } else { throw new ParseException("Too many operands in postfix expression."); } }
private void constructArguments() { while (true) { IToken current; try { current = pollNextInput(); } catch (InvalidOperationException ex) { if (outputCount() > 0) { constructSingleArgument(); } if (processedArguments.Count() < argumentsNeeded) { throw new ParseException("Too few arguments successfully parsed."); } return; } if (canProduceValue(current)) { // LexemeTypes: REAL_VALUE, IDENTIFIER_CONST and IDENTIFIER_VAR are processed here pushValueProducerToOutput(current); } else if (current is Lexeme) { Lexeme currentLexeme = current as Lexeme; Lexeme.LexemeType type = currentLexeme.Type; if (properties.IsFunctionName(currentLexeme.Value)) { processFunctionNameLexeme(currentLexeme); } else if (type == Lexeme.LexemeType.LEFT_PAREN) { operatorStack.Push(currentLexeme); } else if (type == Lexeme.LexemeType.RIGHT_PAREN) { processRightParenthesisLexeme(currentLexeme); } else if (type == Lexeme.LexemeType.OP_PLUS) { if (canProcessTokenAsUnaryOp()) { pushOperator(new Lexeme(Lexeme.LexemeType.OP_PLUS_UNARY, "+")); } else { pushOperator(currentLexeme); } } else if (type == Lexeme.LexemeType.OP_MINUS) { if (canProcessTokenAsUnaryOp()) { pushOperator(new Lexeme(Lexeme.LexemeType.OP_MINUS_UNARY, "-")); } else { pushOperator(currentLexeme); } } else if (type == Lexeme.LexemeType.OP_MUL || type == Lexeme.LexemeType.OP_DIV || type == Lexeme.LexemeType.OP_POW) { pushOperator(currentLexeme); } else if (type == Lexeme.LexemeType.ARGUMENT_SEPARATOR) { processArgumentSeparator(); } else { //Lexeme.LexemeType.EQ_SIGN not allow inside argument list throw new ParseException("Unknown token type encountered in input."); } } } }
public ProgToken(Lexeme lex) { innerText = lex.value; type = lex.type; }
private List <ISyntaxUnit> convertToPostfix(TokenList tokens) { this.reset(); populateInputQueue(tokens); while (true) { IToken current; if (canAddImplicitMultiplication()) { current = new Lexeme(Lexeme.LexemeType.OP_MUL, "*"); } else { try { current = pollNextInput(); } catch (InvalidOperationException ex) { while (operatorStack.Count > 0) { Lexeme op = operatorStack.Pop(); output.Enqueue(op); } return(new List <ISyntaxUnit>(output)); } } if (canProduceValue(current)) { // LexemeTypes: REAL_VALUE, IDENTIFIER_CONST and IDENTIFIER_VAR are processed here pushValueProducerToOutput(current); } else if (current is Lexeme) { Lexeme currentLexeme = current as Lexeme; Lexeme.LexemeType type = currentLexeme.Type; if (properties.IsFunctionName(currentLexeme.Value)) { processFunctionNameLexeme(currentLexeme); } else if (type == Lexeme.LexemeType.LEFT_PAREN) { operatorStack.Push(currentLexeme); } else if (type == Lexeme.LexemeType.RIGHT_PAREN) { processRightParenthesisLexeme(currentLexeme); } else if (type == Lexeme.LexemeType.OP_PLUS) { if (canProcessTokenAsUnaryOp()) { pushOperator(new Lexeme(Lexeme.LexemeType.OP_PLUS_UNARY, "+")); } else { pushOperator(currentLexeme); } } else if (type == Lexeme.LexemeType.OP_MINUS) { if (canProcessTokenAsUnaryOp()) { pushOperator(new Lexeme(Lexeme.LexemeType.OP_MINUS_UNARY, "-")); } else { pushOperator(currentLexeme); } } else if (type == Lexeme.LexemeType.OP_MUL || type == Lexeme.LexemeType.OP_DIV || type == Lexeme.LexemeType.OP_POW || type == Lexeme.LexemeType.EQ_SIGN) { pushOperator(currentLexeme); } else if (type == Lexeme.LexemeType.ARGUMENT_SEPARATOR) { processArgumentSeparator(); } else { throw new ParseException("Unknown token type encountered in input."); } } } }