Esempio n. 1
0
        public void Scan()
        {
            while (this.CurrentPosition < this.m_input.Length)
            {
                bool foundMatch = false;
                for (int i = 0; i < this.m_ruleTable.Count; i++)
                {
                    LexRule <ScannerType, TokenType> rule = this.m_ruleTable[i];

                    if (!this.m_state.IsIn(rule.States))
                    {
                        continue;
                    }

                    if (!rule.PreCondition(this.m_scanner))
                    {
                        continue;
                    }

                    foundMatch = TryMatch(rule);
                    if (foundMatch && !rule.ContinueMatching)
                    {
                        break;
                    }
                }

                if (!foundMatch)
                {
                    string msg = StringExtensions.Fi("Failed to find a matching rule while scanning text. The problem ocurred at "
                                                     + "position {0}. The text in the vicinity was:\n{1}", this.CurrentPosition, this.CurrentVicinity);

                    throw (new AssertionViolationException(msg));
                }
            }
        }
Esempio n. 2
0
        private bool TryMatch(LexRule <ScannerType, TokenType> rule)
        {
            Match m = rule.Regex.Match(this.Input, this.CurrentPosition);

            if (m.Success)
            {
                if (rule.Token != null)
                {
                    this.m_tokenQueue.Enqueue(new KeyValuePair <int, TokenType>(rule.TokenAsInt, rule.YYText(m, this.m_scanner)));
                }

                this.CurrentPosition += m.Length;
                rule.Action(m, this.m_scanner);

                return(true);
            }

            return(false);
        }