Exemplo n.º 1
0
 antlr.IToken CreateToken(antlr.IToken prototype, int newTokenType, string newTokenText)
 {
     return(new BooToken(newTokenType, newTokenText,
                         prototype.getFilename(),
                         prototype.getLine(),
                         prototype.getColumn() + SafeGetLength(prototype.getText())));
 }
Exemplo n.º 2
0
        void FlushBuffer(antlr.IToken token)
        {
            if (0 == _buffer.Length)
            {
                return;
            }

            string text = _buffer.ToString();

            string[] lines = text.Split(NewLineCharArray);

            if (lines.Length > 1)
            {
                string lastLine = lines[lines.Length - 1];

                // Protect against mixed indentation issues
                if (String.Empty != lastLine)
                {
                    if (null == _expectedIndent)
                    {
                        _expectedIndent = lastLine.Substring(0, 1);
                    }

                    if (String.Empty != lastLine.Replace(_expectedIndent, String.Empty))
                    {
                        string literal = _expectedIndent == "\t"
                                                               ? "tabs"
                                                               : _expectedIndent == "\f"
                                                               ? "form feeds"  // The lexer allows them :p
                                                               : "spaces";

                        throw new TokenStreamRecognitionException(
                                  new RecognitionException(
                                      "Mixed indentation, expected the use of " + literal,
                                      token.getFilename(),
                                      token.getLine(),
                                      // Point exactly to the first invalid char
                                      lastLine.Length - lastLine.TrimStart(_expectedIndent[0]).Length + 1
                                      )
                                  );
                    }
                }

                if (lastLine.Length > CurrentIndentLevel)
                {
                    EnqueueIndent(token);
                    _indentStack.Push(lastLine.Length);
                }
                else if (lastLine.Length < CurrentIndentLevel)
                {
                    EnqueueEOS(token);
                    do
                    {
                        EnqueueDedent();
                        _indentStack.Pop();
                    }while (lastLine.Length < CurrentIndentLevel);
                }
                else
                {
                    EnqueueEOS(token);
                }
            }
        }