Beispiel #1
0
            public static List<Lexeme> Analyze(String text)
            {
                List<Lexeme> lexemes = new List<Lexeme>();

                State currentState = new DefaultState();

                int line = 1;
                for (int i = 0; i < text.Length; i++)
                {
                    char ch = text[i];

                    StateInfo stateInfo = new StateInfo(line);
                    currentState = currentState.Execute(ch, stateInfo);

                    if (stateInfo.Lexemes.Any())
                    {
                        foreach (Lexeme addedLexeme in stateInfo.Lexemes)
                        {
                            if (addedLexeme is NewLine)
                                line++;

                            // Skip multiple consecutive newlines
                            if (!lexemes.Any() || !(lexemes.Last() is NewLine) || !(addedLexeme is NewLine))
                                lexemes.Add(addedLexeme);
                        }
                    }

                    if (stateInfo.ReReadCharacter)
                        i--;
                }

                lexemes.Add(new EndOfCode(line));

                return lexemes;
            }
Beispiel #2
0
 private Lexeme getLexemeFromCurrentText(StateInfo stateInfo)
 {
     if (Keyword.Check(currentText))
     {
         return new Keyword(currentText, stateInfo.Line);
     }
     else
     {
         return new Identifier(currentText, stateInfo.Line);
     }
 }
Beispiel #3
0
        internal override State Execute(char character, StateInfo stateInfo)
        {
            if (character == '>')
            {
                stateInfo.Lexemes.Add(new Arrow(stateInfo.Line));
                return new DefaultState();
            }

            stateInfo.Lexemes.Add(new InvalidCharacter("-", stateInfo.Line));
            stateInfo.ReReadCharacter = true;
            return new DefaultState();
        }
Beispiel #4
0
        internal override State Execute(char character, StateInfo stateInfo)
        {
            if (char.IsLetter(character) || character == '_')
            {
                currentText += character;
                return new WordState(currentText);
            }

            currentText = currentText.Substring(0, currentText.Length - 1);
            stateInfo.Lexemes.Add(getLexemeFromCurrentText(stateInfo));
            stateInfo.Lexemes.Add(new InvalidCharacter(".", stateInfo.Line));
            stateInfo.ReReadCharacter = true;

            return new DefaultState();
        }
Beispiel #5
0
        internal override State Execute(char character, StateInfo stateInfo)
        {
            if (char.IsLetter(character) || char.IsDigit(character) || character == '_')
            {
                currentText += character;
                return this;
            }

            if (character == '.')
            {
                currentText += ".";
                return new DotState(currentText);
            }

            stateInfo.Lexemes.Add(getLexemeFromCurrentText(stateInfo));
            stateInfo.ReReadCharacter = true;

            return new DefaultState();
        }
        internal override State Execute(char character, StateInfo stateInfo)
        {
            if (isComment)
            {
                if (character == '\n')
                {
                    stateInfo.ReReadCharacter = true;
                    return new DefaultState();
                }

                return this;
            }

            if (character == '/')
            {
                isComment = true;
                return this;
            }

            stateInfo.Lexemes.Add(new InvalidCharacter("/", stateInfo.Line));
            stateInfo.ReReadCharacter = true;
            return new DefaultState();
        }
        internal override State Execute(char character, StateInfo stateInfo)
        {
            if (character == '/')
            {
                return new CommentState();
            }

            if (specialCharacters.Contains(character))
            {
                stateInfo.Lexemes.Add(new Character(character.ToString(), stateInfo.Line));
                return this;
            }

            if (character == '\n')
            {
                stateInfo.Lexemes.Add(new NewLine(stateInfo.Line));
                return this;
            }

            if (char.IsLetter(character) || character == '_')
            {
                return new WordState(character.ToString());
            }

            if (skipCharacters.Contains(character))
            {
                return this;
            }

            if (character == '-')
            {
                return new ArrowState();
            }

            stateInfo.Lexemes.Add(new InvalidCharacter(character.ToString(), stateInfo.Line));
            return this;
        }
Beispiel #8
0
 internal abstract State Execute(char character, StateInfo info);