Exemple #1
0
 public override bool Parse(LexicalState state)
 {
     return ParseAny(
         state,
         SingleLineComment.S,
         DelimitedComment.S);
 }
        public void LexicalState_Uncovered()
        {
            Ensure.Throws(() => new LexicalState(null));

            LexicalState state = new LexicalState("test");
            Assert.IsNull(state.Get("key", 0));
        }
Exemple #3
0
 public override bool Parse(LexicalState state)
 {
     return ParseCharacter(
         state,
         c =>
             (c >= 'a' && c <= 'z')
             || (c >= 'A' && c <= 'Z'));
 }
Exemple #4
0
 public override bool Parse(LexicalState state)
 {
     return ParseAny(
         state,
         Identifier.S,
         Literal.S,
         Keyword.S,
         OperatorOrPunctuator.S);
 }
Exemple #5
0
 public override bool Parse(LexicalState state)
 {
     return ParseAny(
         state,
         PpDeclaration.S,
         PpConditional.S,
         PpLine.S,
         PpDiagnostic.S,
         PpRegion.S,
         PpPragma.S);
 }
Exemple #6
0
        /// <summary>
        /// Tries to parse a single character using specified function.
        /// </summary>
        public bool ParseCharacter(LexicalState state, Func<char, bool> check)
        {
            if (state.IsEndOfData)
                return false;

            if (!check(state.Get(state.Position)))
                return false;

            state.AddIncrement(Key, 1);
            return true;
        }
Exemple #7
0
        /// <summary>
        /// Returns true only if all input data has been successfully parsed.
        /// </summary>
        public static bool ParseFull(this LexicalItem item, LexicalState state)
        {
            bool parsed = item.Parse(state);

            if (!parsed)
                return false;

            if (!state.IsEndOfData)
                return false;

            return true;
        }
 public override bool Parse(LexicalState state)
 {
     return ParseWord(
         state,
         "<<=",
         "??",
         "::",
         "++",
         "--",
         "&&",
         "||",
         "->",
         "==",
         "!=",
         "<=",
         ">=",
         "+=",
         "-=",
         "*=",
         "/=",
         "%=",
         "&=",
         "|=",
         "^=",
         "<<",
         "=>",
         "{",
         "}",
         "[",
         "]",
         "(",
         ")",
         ".",
         ",",
         ":",
         ";",
         "+",
         "-",
         "*",
         "/",
         "%",
         "&",
         "|",
         "^",
         "!",
         "~",
         "=",
         "<",
         ">",
         "?");
 }
 public override bool Parse(LexicalState state)
 {
     return ParseAny(
         state,
         new ParseAll(
             LeftRoundBracketTerminal.S,
             Whitespace.O,
             PpExpression.S,
             Whitespace.O,
             RightRoundBracketTerminal.S),
         ConditionalSymbol.S,
         TrueTerminal.S,
         FalseTerminal.S);
 }
Exemple #10
0
        /// <summary>
        /// Tries to parse specified data as a syntactic item.
        /// </summary>
        public static bool TryParse(SyntacticItem item, string data)
        {
            LexicalState lexicalState = new LexicalState(data);
            if (!Input.S.ParseFull(lexicalState))
                return false;

            SyntacticState syntacticState = new SyntacticState(
                lexicalState.ExtractTokens(),
                data);

            if (!item.ParseFull(syntacticState))
                return false;

            return true;
        }
Exemple #11
0
        /// <summary>
        /// Tries to parse any of specified lexical items.
        /// </summary>
        public bool ParseAny(LexicalState state, params LexicalItem[] parts)
        {
            int index = state.Position;

            foreach (LexicalItem part in parts)
            {
                if (part.Parse(state))
                {
                    state.AddBack(Key, index);
                    return true;
                }
            }

            return false;
        }
        public override bool Parse(LexicalState state)
        {
            int index = state.Position;

            if (!UnicodeEscapeSequence.S.Parse(state))
                return false;

            string sequence = state.Get(UnicodeEscapeSequence.S.Key, index);
            char c = (char)Convert.ToInt32(sequence.Substring(2), 16);

            if (!m_categories.Contains(Char.GetUnicodeCategory(c)))
            {
                state.Reset(index);
                return false;
            }

            state.AddBack(Key, index);
            return true;
        }
Exemple #13
0
        /// <summary>
        /// Tries to parse a consequent number of specified lexical items.
        /// </summary>
        public bool ParseAll(LexicalState state, params LexicalItem[] parts)
        {
            int index = state.Position;

            bool parsed = true;
            foreach (LexicalItem part in parts)
            {
                if (!part.Parse(state))
                {
                    parsed = false;
                    break;
                }
            }

            if (!parsed)
            {
                state.Reset(index);
                return false;
            }

            state.AddBack(Key, index);
            return true;
        }
        public override bool Parse(SyntacticState state)
        {
            if (state.IsEndOfData)
                return false;

            // ensure that identifier is captured
            LexicalEntry entry = state.GetInner(state.InnerPosition);
            if (entry.Key != Identifier.S.Key)
                return false;

            // check that identifier consists only of specified letters
            string checkOuter = state.GetOuter(entry);
            LexicalState checkState = new LexicalState(checkOuter);
            if (!m_check.ParseFull(checkState))
                return false;

            state.AddAbsolute(
                Key,
                state.InnerPosition + 1,
                entry.EndPosition);

            return true;
        }
Exemple #15
0
 /// <summary>
 /// Tries to parse an entity from the specified lexical machine state.
 /// In case of success returns true and advances parsing position.
 /// </summary>
 public abstract bool Parse(LexicalState state);
Exemple #16
0
 /// <summary>
 /// Tries to parse any of specified words.
 /// </summary>
 public bool ParseWord(LexicalState state, params string[] words)
 {
     return ParseWord(state, false, words);
 }
Exemple #17
0
        /// <summary>
        /// Tries to parse any of specified words.
        /// </summary>
        public bool ParseWord(LexicalState state, bool ignoreCase, params string[] words)
        {
            if (state.IsEndOfData)
                return false;

            foreach (string word in words)
            {
                if (state.Position + word.Length > state.Length)
                    continue;

                string captured = state.Get(state.Position, word.Length);
                bool matches = ignoreCase
                    ? String.Compare(captured, word, StringComparison.OrdinalIgnoreCase) == 0
                    : captured == word;

                if (matches)
                {
                    state.AddIncrement(Key, word.Length);
                    return true;
                }
            }

            return false;
        }
Exemple #18
0
        /// <summary>
        /// Tries to parse a batch of similar lexical items with specified delimiter.
        /// </summary>
        public bool ParseMany(LexicalState state, LexicalItem part, LexicalItem delimiter)
        {
            int index = state.Position;

            if (!part.Parse(state))
                return false;

            while (true)
            {
                int lastIndex = state.Position;

                if (delimiter != null)
                {
                    if (!delimiter.Parse(state))
                        break;
                }

                if (!part.Parse(state))
                {
                    state.Reset(lastIndex);
                    break;
                }
            }

            state.AddBack(Key, index);
            return true;
        }
Exemple #19
0
 public override bool Parse(LexicalState state)
 {
     return ParseWord(state, "define");
 }
Exemple #20
0
 public override bool Parse(LexicalState state)
 {
     return ParseWord(
         state,
         "abstract",
         "as",
         "base",
         "bool",
         "break",
         "byte",
         "case",
         "catch",
         "char",
         "checked",
         "class",
         "const",
         "continue",
         "decimal",
         "default",
         "delegate",
         "double",
         "do",
         "else",
         "enum",
         "event",
         "explicit",
         "extern",
         "false",
         "finally",
         "fixed",
         "float",
         "foreach",
         "for",
         "goto",
         "if",
         "implicit",
         "interface",
         "internal",
         "int",
         "in",
         "is",
         "lock",
         "long",
         "namespace",
         "new",
         "null",
         "object",
         "operator",
         "out",
         "override",
         "params",
         "private",
         "protected",
         "public",
         "readonly",
         "ref",
         "return",
         "sbyte",
         "sealed",
         "short",
         "sizeof",
         "stackalloc",
         "static",
         "string",
         "struct",
         "switch",
         "this",
         "throw",
         "true",
         "try",
         "typeof",
         "uint",
         "ulong",
         "unchecked",
         "unsafe",
         "ushort",
         "using",
         "virtual",
         "void",
         "volatile",
         "while");
 }
 public override bool Parse(LexicalState state)
 {
     return ParseWord(state, "checksum");
 }
Exemple #22
0
 /// <summary>
 /// Tries to parse an entity from the specified lexical machine state.
 /// In case of success returns true and advances parsing position.
 /// </summary>
 public override bool Parse(LexicalState state)
 {
     return(ParseCharacter(state, c => m_characters.Contains(c)));
 }
Exemple #23
0
 public override bool Parse(LexicalState state)
 {
     return ParseWord(state, "region");
 }
Exemple #24
0
 public override bool Parse(LexicalState state)
 {
     return ParseWord(state, "endif");
 }
Exemple #25
0
 public override bool Parse(LexicalState state)
 {
     return ParseAny(state, m_items);
 }
Exemple #26
0
 public override bool Parse(LexicalState state)
 {
     return ParseCharacter(state, c => c > 177);
 }
 /// <summary>
 /// Tries to parse an entity from the specified lexical machine state.
 /// In case of success returns true and advances parsing position.
 /// </summary>
 public override bool Parse(LexicalState state)
 {
     return ParseCharacter(state, m_check);
 }
Exemple #28
0
        /// <summary>
        /// Tries to parse all specified items except some others.
        /// </summary>
        public bool ParseExcept(LexicalState state, LexicalItem main, LexicalItem exception)
        {
            int index = state.Position;

            if (!main.Parse(state))
                return false;

            string parsed = state.Get(main.Key, index);

            LexicalState check = new LexicalState(parsed);
            if (exception.Parse(check))
            {
                if (check.IsEndOfData)
                {
                    state.Reset(index);
                    return false;
                }
            }

            state.AddBack(Key, index);
            return true;
        }
Exemple #29
0
 public override bool Parse(LexicalState state)
 {
     return ParseWord(state, "pragma");
 }
Exemple #30
0
 /// <summary>
 /// Tries to parses specified data string into lexical item.
 /// </summary>
 protected override bool Parse(LexicalItem item, string data)
 {
     LexicalState state = new LexicalState(data);
     return item.ParseFull(state);
 }
 /// <summary>
 /// Tries to parse an entity from the specified lexical machine state.
 /// In case of success returns true and advances parsing position.
 /// </summary>
 public override bool Parse(LexicalState state)
 {
     return ParseCharacter(state, c => m_characters.Contains(c));
 }