Beispiel #1
0
        public void WriteXml(XmlWriter writer)
        {
            writer.WriteStartAttribute("TimeInNode");
            writer.WriteValue(TimeInNode.ToString("N") + " usec");
            writer.WriteEndAttribute();

            writer.WriteStartAttribute("IsAMatch");
            writer.WriteValue(IsAMatch);
            writer.WriteEndAttribute();

            writer.WriteStartAttribute("Lexemes");
            if (Lexemes.Any())
            {
                writer.WriteValue(Lexemes.Aggregate((workingOutput, next) => workingOutput + " " + next));
            }
            writer.WriteEndAttribute();

            writer.WriteStartAttribute("Goal");
            writer.WriteValue(TheGoal.Symbol.TheSymbol);
            writer.WriteEndAttribute();

            writer.WriteStartAttribute("GoalRange");
            writer.WriteValue(TheGoal.InputPos + "-" + (TheGoal.InputPos + TheGoal.Length - 1));
            writer.WriteEndAttribute();

            writer.WriteStartAttribute("Rule");
            writer.WriteValue(TheRuleRange == null ? String.Empty : TheRuleRange.TheRule.ToString());
            writer.WriteEndAttribute();

            writer.WriteStartAttribute("RuleRange");
            writer.WriteValue(TheRuleRange == null ? String.Empty : String.Format(" {0}-{1}", TheRuleRange.InputPos, TheRuleRange.InputPos + TheRuleRange.Length - 1));
            writer.WriteEndAttribute();
        }
        private void AddIdentifier(string value)
        {
            if (Identifiers.Any(e => e.Name.Equals(value)))
            {
                return;
            }

            int index = Identifiers.Count + 1;

            Identifiers.Add(new Identifier
            {
                Index = index,
                Type  = Lexemes.Any() ? Lexemes.Last()?.Token ?? "" : "",
                Name  = value
            }
                            );
        }
        public OuterLexemes Parse(string code)
        {
            int lineNumber    = 1;
            var lexemeBuilder = new StringBuilder();

            foreach (string symbol in code.Select(c => c.ToString()))
            {
                int realLineNumber = lineNumber;
                if (symbol.Equals("\n"))
                {
                    lineNumber++;
                }

                if (TrimDelimiters.Contains(symbol) || Delimiters.Contains(symbol))
                {
                    string lexeme = lexemeBuilder.ToString();

                    if (PredefinedWords.Contains(lexeme) || Operators.Contains(lexeme))
                    {
                        AddLexeme(lexeme, realLineNumber);
                    }
                    else if (IsIdentifier(lexeme))
                    {
                        // Duplicated identifier
                        if (Lexemes.Any() && Lexemes.Last().SubString.Equals("var") &&
                            Identifiers.Any(e => e.Name.Equals(lexeme)))
                        {
                            AddError($"Duplicate declaration of {lexeme} identifier", realLineNumber);
                            lexemeBuilder.Clear();
                            continue;
                        }

                        // Usage of undeclared identifier
                        if (Lexemes.Any() && !Lexemes.Last().SubString.Equals("var") &&
                            !Lexemes.Last().SubString.Equals("program") && !Identifiers.Any(e => e.Name.Equals(lexeme)))
                        {
                            AddError($"Usage of undeclared identifier: {lexeme}", realLineNumber);
                            lexemeBuilder.Clear();
                            continue;
                        }

                        AddIdentifier(lexeme);
                        AddLexeme(lexeme, realLineNumber, IdentifierType.Identifier);
                    }
                    else if (IsConstant(lexeme))
                    {
                        AddConstant(lexeme);
                        AddLexeme(lexeme, realLineNumber, IdentifierType.Constant);
                    }
                    else if (!string.IsNullOrEmpty(lexeme))
                    {
                        AddError($"Unknown lexeme: {lexeme}", realLineNumber);
                        lexemeBuilder.Clear();
                        continue;
                    }

                    if (Delimiters.Contains(symbol))
                    {
                        AddLexeme(symbol, realLineNumber);
                    }

                    lexemeBuilder.Clear();
                    continue;
                }

                if (!TrimDelimiters.Contains(symbol))
                {
                    lexemeBuilder.Append(symbol);
                }
            }

            return(new OuterLexemes()
            {
                Identifiers = Identifiers,
                Constants = Constants,
                Lexemes = Lexemes,
                Errors = Errors,
                Grammar = GrammarLexemes
            });
        }