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

            LexicalState state = new LexicalState("test");
            Assert.IsNull(state.Get("key", 0));
        }
Пример #3
0
 public override bool Parse(LexicalState state)
 {
     return ParseCharacter(
         state,
         c =>
             (c >= 'a' && c <= 'z')
             || (c >= 'A' && c <= 'Z'));
 }
Пример #4
0
 public override bool Parse(LexicalState state)
 {
     return ParseAny(
         state,
         Identifier.S,
         Literal.S,
         Keyword.S,
         OperatorOrPunctuator.S);
 }
Пример #5
0
 public override bool Parse(LexicalState state)
 {
     return ParseAny(
         state,
         PpDeclaration.S,
         PpConditional.S,
         PpLine.S,
         PpDiagnostic.S,
         PpRegion.S,
         PpPragma.S);
 }
Пример #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;
        }
Пример #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;
        }
Пример #8
0
 public override bool Parse(LexicalState state)
 {
     return ParseWord(
         state,
         "<<=",
         "??",
         "::",
         "++",
         "--",
         "&&",
         "||",
         "->",
         "==",
         "!=",
         "<=",
         ">=",
         "+=",
         "-=",
         "*=",
         "/=",
         "%=",
         "&=",
         "|=",
         "^=",
         "<<",
         "=>",
         "{",
         "}",
         "[",
         "]",
         "(",
         ")",
         ".",
         ",",
         ":",
         ";",
         "+",
         "-",
         "*",
         "/",
         "%",
         "&",
         "|",
         "^",
         "!",
         "~",
         "=",
         "<",
         ">",
         "?");
 }
Пример #9
0
 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);
 }
Пример #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;
        }
Пример #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;
        }
Пример #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;
        }
Пример #14
0
        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;
        }
Пример #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);
Пример #16
0
 /// <summary>
 /// Tries to parse any of specified words.
 /// </summary>
 public bool ParseWord(LexicalState state, params string[] words)
 {
     return ParseWord(state, false, words);
 }
Пример #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;
        }
Пример #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;
        }
Пример #19
0
 public override bool Parse(LexicalState state)
 {
     return ParseWord(state, "define");
 }
Пример #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");
 }
Пример #21
0
 public override bool Parse(LexicalState state)
 {
     return ParseWord(state, "checksum");
 }
Пример #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)));
 }
Пример #23
0
 public override bool Parse(LexicalState state)
 {
     return ParseWord(state, "region");
 }
Пример #24
0
 public override bool Parse(LexicalState state)
 {
     return ParseWord(state, "endif");
 }
Пример #25
0
 public override bool Parse(LexicalState state)
 {
     return ParseAny(state, m_items);
 }
Пример #26
0
 public override bool Parse(LexicalState state)
 {
     return ParseCharacter(state, c => c > 177);
 }
Пример #27
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, m_check);
 }
Пример #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;
        }
Пример #29
0
 public override bool Parse(LexicalState state)
 {
     return ParseWord(state, "pragma");
 }
Пример #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);
 }
Пример #31
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));
 }