public static CompilationMessage Create(ITokenStream source, Interval errorLocation, MessageCode code, string file,
                                                string helpfullmessage   = null,
                                                MessageSeverity severity = MessageSeverity.Error)
        {
            if (source == null)
            {
                throw new ArgumentNullException(nameof(source));
            }
            if (errorLocation.Equals(Interval.Invalid))
            {
                throw new ArgumentOutOfRangeException(nameof(errorLocation));
            }

            //Interval of 10 before, 10 after error
            //TODO: check against 0, source.Lenght
            Interval before = new Interval(Math.Max(errorLocation.a - 11, 0), Math.Max(errorLocation.a - 1, 0));
            Interval after  = new Interval(Math.Min(errorLocation.b + 1, source.Size), Math.Min(errorLocation.b + 11, source.Size));

            string beforestring = source.GetText(before);
            string error        = source.GetText(errorLocation);
            string afterstring  = source.GetText(after);


            return(new CompilationMessage(severity, code, beforestring, error, afterstring, helpfullmessage, file, errorLocation.a));
        }
        public override void EnterAssign(EnumParser.AssignContext context)
        {
            ITokenStream tokens = (ITokenStream)parser.InputStream;
            String       args   = tokens.GetText(context.expr());

            builder.AppendLine("\t" + context.ID() + ": " + args + ",");
        }
Exemplo n.º 3
0
        protected virtual string ErrorMessageForNoViableAlternative(NoViableAltException e, Antlr4.Runtime.Parser recognizer)
        {
            ITokenStream tokens = ((ITokenStream)recognizer.InputStream);
            string       input;

            if (tokens != null)
            {
                if (e.StartToken.Type == TokenConstants.Eof)
                {
                    input = "<end of file>";
                }
                else
                {
                    input = tokens.GetText(e.StartToken, e.OffendingToken);
                }
            }
            else
            {
                input = "<unknown input>";
            }
            string msg = "no viable alternative at input " + EscapeWSAndQuote(input);

            //TEMPORARY FIX until #554 is done
            if (input == "occurs")
            {
                msg = EscapeWSAndQuote(input) + " is not supported yet";
            }

            return(msg);
        }
Exemplo n.º 4
0
        /// <summary>
        /// This is called by
        /// <see cref="ReportError(Parser, RecognitionException)">ReportError(Parser, RecognitionException)
        ///     </see>
        /// when the exception is a
        /// <see cref="NoViableAltException">NoViableAltException</see>
        /// .
        /// </summary>
        /// <seealso cref="ReportError(Parser, RecognitionException)">ReportError(Parser, RecognitionException)
        ///     </seealso>
        /// <param name="recognizer">the parser instance</param>
        /// <param name="e">the recognition exception</param>
        protected internal virtual void ReportNoViableAlternative(Parser recognizer, NoViableAltException
                                                                  e)
        {
            ITokenStream tokens = ((ITokenStream)recognizer.InputStream);
            string       input;

            if (tokens is ITokenStream)
            {
                if (e.GetStartToken().Type == TokenConstants.Eof)
                {
                    input = "<EOF>";
                }
                else
                {
                    input = tokens.GetText(e.GetStartToken(), e.OffendingToken);
                }
            }
            else
            {
                input = "<unknown input>";
            }
            string msg = "no viable alternative at input " + EscapeWSAndQuote(input);

            NotifyErrorListeners(recognizer, msg, e);
        }
Exemplo n.º 5
0
    public static boolean isOperator(ITokenStream tokens, String op)
    {
        var stop = getLastOpTokenIndex(tokens);

        if (stop == -1)
        {
            return(false);
        }

        var start = tokens.Index;
        var text  = tokens.GetText(Interval.Of(start, stop));

        return(text.Equals(op));
    }
Exemplo n.º 6
0
    public static boolean isOpNext(ITokenStream tokens)
    {
        var start = tokens.Index;
        var lt    = tokens.Get(start);
        var stop  = getLastOpTokenIndex(tokens);

        if (stop == -1)
        {
            return(false);
        }
        Console.WriteLine("isOpNext: i={0} t='{1}'", start, lt.getText());
        Console.WriteLine(", op='{0}'\n", tokens.GetText(Interval.Of(start, stop)));
        return(true);
    }
Exemplo n.º 7
0
        public override void EnterAssign(EnumParser.AssignContext context)
        {
            ITokenStream tokens = (ITokenStream)parser.InputStream;
            String       args;

            if (context.expr() != null)
            {
                args = tokens.GetText(context.expr());
            }
            else
            {
                args = currentlyParsingEnum.ParsedEnumIds.Count().ToString();
            }

            string name = Char.ToLowerInvariant(context.ID().ToString()[0]) + context.ID().ToString().Substring(1);

            currentlyParsingEnum.ParsedEnumIds.Add(Int32.Parse(args));
            currentlyParsingEnum.ParsedEnumContent.AppendLine("\t" + name + ": " + args + ",");
        }
Exemplo n.º 8
0
    /**
     * "If an operator has whitespace on the left side only, it is treated as a
     * prefix unary operator. As an example, the ++ operator in a ++b is treated
     * as a prefix unary operator."
     */

    public static boolean isPrefixOp(ITokenStream tokens)
    {
        int stop = getLastOpTokenIndex(tokens);

        if (stop == -1)
        {
            return(false);
        }

        var start     = tokens.Index;
        var prevToken = tokens.get(start - 1); // includes hidden-channel tokens
        var nextToken = tokens.get(stop + 1);
        var prevIsWS  = isLeftOperatorWS(prevToken);
        var nextIsWS  = isRightOperatorWS(nextToken);
        var result    = prevIsWS && !nextIsWS;
        var text      = tokens.GetText(Interval.Of(start, stop));

        //System.out.println("isPrefixOp: '"+prevToken+"','"+text+"','"+nextToken+"' is "+result);
        return(result);
    }
Exemplo n.º 9
0
        protected override void ReportNoViableAlternative(Parser recognizer, NoViableAltException e)
        {
            ITokenStream tokens = ((ITokenStream)recognizer.InputStream);
            string       input;

            if (tokens != null)
            {
                if (e.StartToken.Type == TokenConstants.EOF)
                {
                    input = "<EOF>";
                }
                else
                {
                    input = tokens.GetText(e.StartToken, e.OffendingToken);
                }
            }
            else
            {
                input = "<unknown input>";
            }
            string msg = "无法决定采用哪一条匹配路径 " + EscapeWSAndQuote(input);

            NotifyErrorListeners(recognizer, msg, e);
        }
Exemplo n.º 10
0
        public virtual string GetText(string programName, Interval interval)
        {
            IList <TokenStreamRewriter.RewriteOperation> rewrites;

            if (!programs.TryGetValue(programName, out rewrites))
            {
                rewrites = null;
            }

            int start = interval.a;
            int stop  = interval.b;

            // ensure start/end are in range
            if (stop > tokens.Size - 1)
            {
                stop = tokens.Size - 1;
            }
            if (start < 0)
            {
                start = 0;
            }
            if (rewrites == null || rewrites.Count == 0)
            {
                return(tokens.GetText(interval));
            }
            // no instructions to execute
            StringBuilder buf = new StringBuilder();
            // First, optimize instruction stream
            IDictionary <int, TokenStreamRewriter.RewriteOperation> indexToOp = ReduceToSingleOperationPerIndex(rewrites);
            // Walk buffer, executing instructions and emitting tokens
            int i = start;

            while (i <= stop && i < tokens.Size)
            {
                TokenStreamRewriter.RewriteOperation op;
                if (indexToOp.TryGetValue(i, out op))
                {
                    indexToOp.Remove(i);
                }

                // remove so any left have index size-1
                IToken t = tokens.Get(i);
                if (op == null)
                {
                    // no operation at that index, just dump token
                    if (t.Type != TokenConstants.EOF)
                    {
                        buf.Append(t.Text);
                    }
                    i++;
                }
                else
                {
                    // move to next token
                    i = op.Execute(buf);
                }
            }
            // execute operation and skip
            // include stuff after end if it's last index in buffer
            // So, if they did an insertAfter(lastValidIndex, "foo"), include
            // foo if end==lastValidIndex.
            if (stop == tokens.Size - 1)
            {
                // Scan any remaining operations after last token
                // should be included (they will be inserts).
                foreach (TokenStreamRewriter.RewriteOperation op in indexToOp.Values)
                {
                    if (op.index >= tokens.Size - 1)
                    {
                        buf.Append(op.text);
                    }
                }
            }
            return(buf.ToString());
        }
Exemplo n.º 11
0
 public static string GetText(this ITokenStream tokenStream, int startIndex, int stopIndex)
 {
     var interval = new Interval(startIndex, stopIndex);
     return tokenStream.GetText(interval);
 }
Exemplo n.º 12
0
        private static string RewriteEPL(
            IList<UniformPair<int>> ranges,
            ITokenStream tokens)
        {
            if (ranges.IsEmpty())
            {
                return tokens.GetText();
            }

            var writer = new StringWriter();
            var rangeIndex = 0;
            var current = ranges[rangeIndex];
            for (var i = 0; i < tokens.Size; i++)
            {
                var t = tokens.Get(i);
                if (t.Type == EsperEPL2GrammarLexer.Eof)
                {
                    break;
                }

                if (i < current.First)
                {
                    writer.Write(t.Text);
                }
                else if (i == current.First)
                {
                    writer.Write(t.Text);
                    writer.Write("'");
                }
                else if (i == current.Second)
                {
                    writer.Write("'");
                    writer.Write(t.Text);
                    rangeIndex++;
                    if (ranges.Count > rangeIndex)
                    {
                        current = ranges[rangeIndex];
                    }
                    else
                    {
                        current = new UniformPair<int>(-1, -1);
                    }
                }
                else if (t.Type == EsperEPL2GrammarParser.SL_COMMENT || t.Type == EsperEPL2GrammarParser.ML_COMMENT)
                {
                    WriteCommentEscapeSingleQuote(writer, t);
                }
                else
                {
                    if (t.Type == EsperEPL2GrammarParser.QUOTED_STRING_LITERAL && i > current.First && i < current.Second)
                    {
                        writer.Write("\\'");
                        writer.Write(t.Text.Substring(1, t.Text.Length - 2));
                        writer.Write("\\'");
                    }
                    else if (t.Type == EsperEPL2GrammarParser.STRING_LITERAL)
                    {
                        writer.Write(t.Text.Replace("'", "\\'"));
                    }
                    else
                    {
                        writer.Write(t.Text);
                    }
                }
            }

            return writer.ToString();
        }
Exemplo n.º 13
0
        public void EnterUsingNamespaceDirective(UsingNamespaceDirectiveContext context)
        {
            var text = _tokenStream.GetText(context.Start, context.Stop);

            _interfacePreProcTemplate.AppendLine(text);
        }
Exemplo n.º 14
0
    public static boolean isOperator(ITokenStream tokens, String op) {
        var stop = getLastOpTokenIndex(tokens);
        if (stop == -1) return false;

        var start = tokens.Index;
        var text = tokens.GetText(Interval.Of(start, stop));
        return text.Equals(op);
    }
Exemplo n.º 15
0
    /**
     "If an operator has whitespace on the right side only, it is treated as a
     postfix unary operator. As an example, the ++ operator in a++ b is treated
     as a postfix unary operator."

     "If an operator has no whitespace on the left but is followed immediately
     by a dot (.), it is treated as a postfix unary operator. As an example,
     the ++ operator in a++.b is treated as a postfix unary operator (a++ .b
     rather than a ++ .b)."
     */

    public static boolean isPostfixOp(ITokenStream tokens) {
        int stop = getLastOpTokenIndex(tokens);
        if (stop == -1) return false;

        int start = tokens.Index;
        var prevToken = tokens.get(start - 1); // includes hidden-channel tokens
        var nextToken = tokens.get(stop + 1);
        var prevIsWS = isLeftOperatorWS(prevToken);
        var nextIsWS = isRightOperatorWS(nextToken);
        var result =
                !prevIsWS && nextIsWS ||
                !prevIsWS && nextToken.getType() == SwiftParser.DOT;
        var text = tokens.GetText(Interval.Of(start, stop));
        //System.out.println("isPostfixOp: '"+prevToken+"','"+text+"','"+nextToken+"' is "+result);
        return result;
    }
Exemplo n.º 16
0
 public static boolean isOpNext(ITokenStream tokens) {
     var start = tokens.Index;
     var lt = tokens.Get(start);
     var stop = getLastOpTokenIndex(tokens);
     if (stop == -1) return false;
     Console.WriteLine("isOpNext: i={0} t='{1}'", start, lt.getText());
     Console.WriteLine(", op='{0}'\n", tokens.GetText(Interval.Of(start, stop)));
     return true;
 }