Ejemplo n.º 1
0
 private ConstraintParserState ParseFirstChar(ConstraintParserState state, char ch)
 {
     if (ch == '\0')
     {
         if (eqRelationDefined)
         {
             AppendTerm();
             return(ConstraintParserState.done);
         }
         else
         {
             errorMessage = "End of expression is reached, but operator is not defined";
             return(ConstraintParserState.syntaxError);
         }
     }
     else if (char.IsDigit(ch))
     {
         word.Append(ch);
         return(ConstraintParserState.leadNumber);
     }
     else if (char.IsLetter(ch))
     {
         word.Append(ch);
         return(ConstraintParserState.word);
     }
     else if (IsMultiplicationOperator(ch))
     {
         SetMultipler(true);
         segmentOp = ch;
         return(ConstraintParserState.word);
     }
     else if (IsAditionOperator(ch))
     {
         AppendTerm();
         segmentSign = ch;
         return(ConstraintParserState.firstChar);
     }
     else if (IsEqualityOperator(ch))
     {
         if (!eqRelationDefined)
         {
             AppendTerm();
             SetEquationSign(ch);
             return(ConstraintParserState.equality);
         }
         else
         {
             errorMessage = "Operator already defined";
             return(ConstraintParserState.syntaxError);
         }
     }
     else
     {
         errorMessage = "Unexpected symbol";
         return(ConstraintParserState.syntaxError);
     }
 }
Ejemplo n.º 2
0
 private ConstraintParserState ParseEquality(ConstraintParserState state, char ch)
 {
     if (ch == '=')
     {
         return(ConstraintParserState.firstChar);
     }
     else
     {
         errorMessage = "Symbol '=' expected";
         return(ConstraintParserState.syntaxError);
     }
 }
Ejemplo n.º 3
0
 public ConstraintParser()
 {
     state      = ConstraintParserState.word;
     stateTable = new Dictionary <ConstraintParserState, Func <ConstraintParserState, char, ConstraintParserState> >
     {
         { ConstraintParserState.firstChar, ParseFirstChar },
         { ConstraintParserState.leadNumber, ParseLeadNumber },
         { ConstraintParserState.word, ParseWord },
         { ConstraintParserState.anchor, ParseAnchor },
         { ConstraintParserState.number, ParseNumber },
         { ConstraintParserState.equality, ParseEquality },
         { ConstraintParserState.done, ParseDone },
     };
 }
Ejemplo n.º 4
0
        public ConstraintDescription Parse(string constraint)
        {
            state = ConstraintParserState.firstChar;
            word.Clear();
            prototype         = new ConstraintDescription();
            prototype.Terms   = new List <ConstraintSegment>();
            errorMessage      = null;
            hasLeadNumber     = false;
            eqRelationDefined = false;

            int charNumber = 0;

            foreach (var ch in constraint.Append('\0'))
            {
                if (char.IsWhiteSpace(ch))
                {
                    charNumber++;
                    continue;
                }

                state = stateTable[state](state, ch);
                if (state == ConstraintParserState.syntaxError)
                {
                    throw new SyntaxErrorException($"{errorMessage} at char {charNumber}");
                }
                charNumber++;
            }

            if (!prototype.Terms.Any(x => x.OwnerId != null))
            {
                throw new SemanticErrorException($"parsed expression does not contains anchors");
            }

            if (state != ConstraintParserState.done)
            {
                throw new InternalErrorException($"parser ended work in invalid state: {state}");
            }


            return(prototype);
        }
Ejemplo n.º 5
0
 private ConstraintParserState ParseWord(ConstraintParserState state, char ch)
 {
     if (ch == '\0')
     {
         errorMessage = "Unexpected end of expression";
         return(ConstraintParserState.syntaxError);
     }
     else if (char.IsLetterOrDigit(ch))
     {
         word.Append(ch);
         return(ConstraintParserState.word);
     }
     else if (ch == '.')
     {
         SetTarget();
         return(ConstraintParserState.anchor);
     }
     else
     {
         errorMessage = "Anchor name expected";
         return(ConstraintParserState.syntaxError);
     }
 }
Ejemplo n.º 6
0
 private ConstraintParserState ParseDone(ConstraintParserState state, char ch)
 {
     errorMessage = "symbol after expression end";
     return(ConstraintParserState.syntaxError); // we are ended parsing, there shouldn't be any characters after that
 }