Пример #1
0
 public SBExpressionData(
     HomeType home = HomeType.Immediate,
     SBExpressionType referencedtype = SBExpressionType.Expression,
     TypeReference type          = null,
     Expression code             = null,
     object value                = null,
     string argument             = null,
     string parameterName        = null,
     Expression instance         = null,
     bool automaticTypeConvert   = false,
     Antlr4.Runtime.IToken token = null)
 {
     System.Diagnostics.Debug.Assert(type == null || !type.Type.IsGenericTypeDefinition);
     System.Diagnostics.Debug.Assert(type == null || !type.Type.IsGenericParameter);
     System.Diagnostics.Debug.Assert(value == null || !(value is IFileProcedure));
     Home           = home;
     ReferencedType = referencedtype;
     DataType       = type ?? (code != null ? (TypeReference)(code.Type) : null);
     ExpressionCode = code;
     Value          = value;
     Argument       = argument;
     ParameterName  = parameterName;
     InstanceCode   = instance;
     SuggestsAutomaticTypeConversion = automaticTypeConvert;
     Token = token;
 }
Пример #2
0
        public override ASTNode VisitDeclaration([NotNull] p4Parser.DeclarationContext context)
        {
            IdAstNode id;

            if (context.Assign() != null)
            {
                Antlr4.Runtime.IToken symbol = context.Assign().Symbol;
                ASTNode expr = Visit(context.expr());
                if (context.modifier() != null)
                {
                    id = new IdAstNode(context.Id().Symbol, SALType.Types[Visit(context.valuetype()).Token.Text], Visit(context.modifier()).Token);
                    return(new AssignAstNode(id, symbol, expr));
                }
                else
                {
                    id = new IdAstNode(context.Id().Symbol, SALType.Types[Visit(context.valuetype()).Token.Text]);
                    return(new AssignAstNode(id, symbol, expr));
                }
            }
            else
            {
                if (context.modifier() != null)
                {
                    id = new IdAstNode(context.Id().Symbol, SALType.Types[Visit(context.valuetype()).Token.Text], Visit(context.modifier()).Token);
                    return(new DeclareAstNode(id, null));
                }
                else
                {
                    id = new IdAstNode(context.Id().Symbol, SALType.Types[Visit(context.valuetype()).Token.Text]);
                    return(new DeclareAstNode(id, null));
                }
            }
        }
Пример #3
0
        public override void SyntaxError(IRecognizer recognizer,
                                         IToken offendingSymbol, int line, int charPositionInLine, string msg, RecognitionException e)
        {
            MainForm f  = (MainForm)Application.OpenForms[0];
            TextBox  tx = f.resultBox;

            tx.Text += Environment.NewLine + Environment.NewLine + "> line " + line.ToString() + ":" + charPositionInLine.ToString() + " at " + offendingSymbol.ToString() + ": " + msg;
        }
Пример #4
0
 public override void SyntaxError(IRecognizer recognizer,
     IToken offendingSymbol, int line, int charPositionInLine, string msg, RecognitionException e)
 {
     MainForm f = (MainForm)Application.OpenForms[0];
     TextBox tx = f.resultBox;
     tx.Text += Environment.NewLine + Environment.NewLine + "> line " + line.ToString() + ":" + charPositionInLine.ToString() + " at " + offendingSymbol.ToString() + ": " + msg;
        
 }
 public SyntaxEditorAntlrToken(Antlr.IToken token, int offset = 0, int lineOffset = 0) :
     base(
         token.StartIndex + offset,
         (token.StopIndex - token.StartIndex) + 1,
         new TextPosition(token.Line + lineOffset, token.Column + 1),
         new TextPosition(token.Line + lineOffset, token.Column + (token.StopIndex - token.StartIndex) + 1))
 {
     m_token = token;
 }
Пример #6
0
        public override ASTNode VisitAssignment([NotNull] p4Parser.AssignmentContext context)
        {
            IdAstNode id = new IdAstNode(context.Id().Symbol, SALTypeEnum.undefined);

            Antlr4.Runtime.IToken symbol = Visit(context.assnOp()).Token;
            ASTNode expr = Visit(context.expr());

            return(new AssignAstNode(id, symbol, expr));
        }
Пример #7
0
 protected void processNEWLINEToken()
 {
     if (pendingDent)
     {
         base.Channel = Hidden;
     }
     indentCount        = 0;
     initialIndentToken = null;
 }
Пример #8
0
 public SBExpressionData(SBExpressionType type, string message, string value, Antlr4.Runtime.IToken token = null)
 {
     Home           = HomeType.Immediate;
     ReferencedType = type;
     DataType       = null;
     ExpressionCode = null;
     Value          = value;
     Argument       = message;
     ParameterName  = null;
     Token          = token;
 }
Пример #9
0
 public SBExpressionData(TypeReference type, object value, Antlr4.Runtime.IToken token = null)
 {
     Home           = HomeType.Immediate;
     ReferencedType = SBExpressionType.Constant;
     DataType       = type;
     ExpressionCode = Expression.Constant(value, type.Type);
     Value          = value;
     Argument       = null;
     ParameterName  = null;
     Token          = token;
 }
Пример #10
0
        createToken(int type, String text, IToken next)
        {
            CommonToken token = new CommonToken(type, text);

            if (initialIndentToken != null)
            {
                token.StartIndex = initialIndentToken.StartIndex;
                token.Line       = initialIndentToken.Line;
                token.Column     = initialIndentToken.Column;
                token.StopIndex  = next.StartIndex - 1;
            }
            return(token);
        }
Пример #11
0
 public static SBExpressionData Constant(TypeReference type, object value, Antlr4.Runtime.IToken token = null)
 {
     System.Diagnostics.Debug.Assert(!type.Type.IsGenericTypeDefinition);
     System.Diagnostics.Debug.Assert(!type.Type.IsGenericParameter);
     System.Diagnostics.Debug.Assert(value == null || !(value is IFileProcedure));
     return(new SBExpressionData(
                HomeType.Immediate,
                SBExpressionType.Constant,
                type,
                Expression.Constant(value, type.Type),
                value,
                token: token));
 }
Пример #12
0
 public SBExpressionData(object value, Antlr4.Runtime.IToken token = null)
 {
     if (value == null)
     {
         throw new ArgumentNullException();
     }
     Home           = HomeType.Immediate;
     ReferencedType = SBExpressionType.Constant;
     DataType       = (TypeReference)(value.GetType());
     ExpressionCode = Expression.Constant(value);
     Value          = value;
     Argument       = null;
     ParameterName  = null;
     Token          = token;
 }
Пример #13
0
        private void processINToken(IToken next)
        {
            while (!indentStack.IsEmpty() && indentStack.Peek().first() != "let")
            {
                tokenQueue.AddLast(new LinkedListNode <IToken>(createToken(GHaskellLexer.SEMI, "SEMI", next)));
                tokenQueue.AddLast(new LinkedListNode <IToken>(createToken(GHaskellLexer.VCCURLY, "VCCURLY", next)));
                nestedLevel--;
                indentStack.Pop();
            }

            if (!indentStack.IsEmpty() && indentStack.Peek().first() == "let")
            {
                tokenQueue.AddLast(new LinkedListNode <IToken>(createToken(GHaskellLexer.SEMI, "SEMI", next)));
                tokenQueue.AddLast(new LinkedListNode <IToken>(createToken(GHaskellLexer.VCCURLY, "VCCURLY", next)));
                nestedLevel--;
                indentStack.Pop();
            }
        }
Пример #14
0
        private void processEofToken(IToken next)
        {
            indentCount = startIndent;
            if (!pendingDent)
            {
                initialIndentToken = next;
            }

            while (nestedLevel > indentStack.Count)
            {
                if (nestedLevel > 0)
                {
                    nestedLevel--;
                }

                tokenQueue.AddLast(new LinkedListNode <IToken>(createToken(GHaskellLexer.SEMI, "SEMI", next)));
                tokenQueue.AddLast(new LinkedListNode <IToken>(createToken(GHaskellLexer.VCCURLY, "VCCURLY", next)));
            }

            while (indentCount < getSavedIndent())
            {
                if (!indentStack.IsEmpty() && nestedLevel > 0)
                {
                    indentStack.Pop();
                    nestedLevel--;
                }

                tokenQueue.AddLast(new LinkedListNode <IToken>(createToken(GHaskellLexer.SEMI, "SEMI", next)));
                tokenQueue.AddLast(new LinkedListNode <IToken>(createToken(GHaskellLexer.VCCURLY, "VCCURLY", next)));
            }

            if (indentCount == getSavedIndent())
            {
                tokenQueue.AddLast(new LinkedListNode <IToken>(createToken(GHaskellLexer.SEMI, "SEMI", next)));
            }

            if (wasModuleExport)
            {
                tokenQueue.AddLast(new LinkedListNode <IToken>(createToken(GHaskellLexer.VCCURLY, "VCCURLY", next)));
            }

            startIndent = -1;
        }
Пример #15
0
 /// <summary>
 /// Checking whether in the original statement there is a whitespace on the right hand of a token. If there is returns true
 /// - useful to restore the whitespace to get the original statement (or modified one) back
 /// </summary>
 /// <param name="idx">Token index</param>
 /// <returns>true if there is a whitespace</returns>
 bool whiteSpaceOnRight(int idx)
 {
     try
     {
         IList <IToken> WSChannel = this.tokens.GetHiddenTokensToRight(idx, ParamExprGrammarLexer.WHITESPACE);
         if (WSChannel != null)
         {
             IToken ws = WSChannel.First();
             if (ws != null)
             {
                 return(true);
             }
         }
         return(false);
     }
     catch (ArgumentOutOfRangeException)
     {
         // Ignore?
         return(false);
     }
 }
Пример #16
0
 public void UnresolvedIdentifier(Antlr4.Runtime.IToken token, string name)
 {
     this.SymanticError((token != null) ? token.Line : -1, (token != null) ? token.Column : -1, false, $"Unresolved identifier: \"{name}\".");
 }
Пример #17
0
 private void LogError(Antlr4.Runtime.IToken token, string str)
 {
     mLogger.Error($"({token.Line:D4}:{token.Column:D4}) {str}");
 }
Пример #18
0
 public override void SyntaxError(System.IO.TextWriter output, Antlr4.Runtime.IRecognizer recognizer, Antlr4.Runtime.IToken offendingSymbol, int line, int charPositionInLine, string msg, Antlr4.Runtime.RecognitionException e)
 {
     this.vm.set_runable(false);
     //Console.WriteLine(e.GetType().Name);
     Console.WriteLine("SiBty error ({0}:{1}) -> {2}", line, charPositionInLine, msg);
     this.under_line(line, charPositionInLine);
 }
Пример #19
0
        // Algorithm's description here:
        // https://www.haskell.org/onlinereport/haskell2010/haskellch10.html
        // https://en.wikibooks.org/wiki/Haskell/Indentation
        public override IToken NextToken()
        {
            if (!tokenQueue.IsEmpty())
            {
                return(tokenQueue.First.Value);
            }

            IToken next = base.NextToken();
            int    type = next.Type;

            if (startIndent == -1 &&
                type != GHaskellLexer.NEWLINE &&
                type != GHaskellLexer.WS &&
                type != GHaskellLexer.TAB &&
                type != GHaskellLexer.OCURLY)
            {
                if (type == GHaskellLexer.MODULE)
                {
                    moduleStartIndent = true;
                    wasModuleExport   = true;
                }
                if (type != GHaskellLexer.MODULE && !moduleStartIndent)
                {
                    startIndent = next.Column;
                }
                else if (lastKeyWord == "where" && moduleStartIndent)
                {
                    lastKeyWord       = "";
                    prevWasKeyWord    = false;
                    nestedLevel       = 0;
                    moduleStartIndent = false;
                    prevWasEndl       = false;
                    startIndent       = next.Column;
                    tokenQueue.AddLast(new LinkedListNode <IToken>(createToken(GHaskellLexer.VOCURLY, "VOCURLY", next)));
                    tokenQueue.AddLast(new LinkedListNode <IToken>(createToken(type, next.Text, next)));

                    return(tokenQueue.First.Value);
                }
            }

            if (type == GHaskellLexer.OCURLY)
            {
                if (prevWasKeyWord)
                {
                    nestedLevel--;
                    prevWasKeyWord = false;
                }

                if (moduleStartIndent)
                {
                    moduleStartIndent = false;
                    // because will be  GHaskellLexer.CCURLY in the end of file
                    wasModuleExport = false;
                }

                ignoreIndent = true;
                prevWasEndl  = false;
            }

            if (prevWasKeyWord && !prevWasEndl &&
                !moduleStartIndent &&
                type != GHaskellLexer.WS &&
                type != GHaskellLexer.NEWLINE &&
                type != GHaskellLexer.TAB &&
                type != GHaskellLexer.OCURLY)
            {
                prevWasKeyWord = false;
                indentStack.Push(new Pair <String, int>(lastKeyWord, next.Column));
                tokenQueue.AddLast(new LinkedListNode <IToken>(createToken(GHaskellLexer.VOCURLY, "VOCURLY", next)));
            }

            if (ignoreIndent &&
                (type == GHaskellLexer.WHERE ||
                 type == GHaskellLexer.DO ||
                 type == GHaskellLexer.LET ||
                 type == GHaskellLexer.OF ||
                 type == GHaskellLexer.CCURLY)
                )
            {
                ignoreIndent = false;
            }

            if (pendingDent &&
                prevWasKeyWord &&
                !ignoreIndent &&
                indentCount <= getSavedIndent() &&
                type != GHaskellLexer.NEWLINE &&
                type != GHaskellLexer.WS)
            {
                tokenQueue.AddLast(new LinkedListNode <IToken>(createToken(GHaskellLexer.VOCURLY, "VOCURLY", next)));
                prevWasKeyWord = false;
                prevWasEndl    = true;
            }


            if (pendingDent && prevWasEndl &&
                !ignoreIndent &&
                indentCount <= getSavedIndent() &&
                type != GHaskellLexer.NEWLINE &&
                type != GHaskellLexer.WS &&
                type != GHaskellLexer.WHERE &&
                type != GHaskellLexer.IN &&
                type != GHaskellLexer.DO &&
                type != GHaskellLexer.OF &&
                type != GHaskellLexer.CCURLY &&
                type != Eof)
            {
                while (nestedLevel > indentStack.Count)
                {
                    if (nestedLevel > 0)
                    {
                        nestedLevel--;
                    }

                    tokenQueue.AddLast(new LinkedListNode <IToken>(createToken(GHaskellLexer.SEMI, "SEMI", next)));
                    tokenQueue.AddLast(new LinkedListNode <IToken>(createToken(GHaskellLexer.VCCURLY, "VCCURLY", next)));
                }

                while (indentCount < getSavedIndent())
                {
                    if (!indentStack.IsEmpty() && nestedLevel > 0)
                    {
                        indentStack.Pop();
                        nestedLevel--;
                    }

                    tokenQueue.AddLast(new LinkedListNode <IToken>(createToken(GHaskellLexer.SEMI, "SEMI", next)));
                    tokenQueue.AddLast(new LinkedListNode <IToken>(createToken(GHaskellLexer.VCCURLY, "VCCURLY", next)));
                }

                if (indentCount == getSavedIndent())
                {
                    tokenQueue.AddLast(new LinkedListNode <IToken>(createToken(GHaskellLexer.SEMI, "SEMI", next)));
                }

                prevWasEndl = false;
                if (indentCount == startIndent)
                {
                    pendingDent = false;
                }
            }


            if (pendingDent && prevWasKeyWord &&
                !moduleStartIndent &&
                !ignoreIndent &&
                indentCount > getSavedIndent() &&
                type != GHaskellLexer.NEWLINE &&
                type != GHaskellLexer.WS &&
                type != Eof)
            {
                prevWasKeyWord = false;

                if (prevWasEndl)
                {
                    indentStack.Push(new Pair <String, int>(lastKeyWord, indentCount));
                    prevWasEndl = false;
                }

                tokenQueue.AddLast(new LinkedListNode <IToken>(createToken(GHaskellLexer.VOCURLY, "VOCURLY", next)));
            }

            if (pendingDent &&
                initialIndentToken == null &&
                GHaskellLexer.NEWLINE != type)
            {
                initialIndentToken = next;
            }

            if (next != null && type == GHaskellLexer.NEWLINE)
            {
                prevWasEndl = true;
            }

            if (type == GHaskellLexer.WHERE ||
                type == GHaskellLexer.LET ||
                type == GHaskellLexer.DO ||
                type == GHaskellLexer.OF)
            {
                // if next will be GHaskellLexer.OCURLY need to decrement nestedLevel
                nestedLevel++;
                prevWasKeyWord = true;
                prevWasEndl    = false;
                lastKeyWord    = next.Text;

                if (type == GHaskellLexer.WHERE)
                {
                    if (!indentStack.IsEmpty() && (indentStack.Peek().first() == "do"))
                    {
                        tokenQueue.AddLast(new LinkedListNode <IToken>(createToken(GHaskellLexer.SEMI, "SEMI", next)));
                        tokenQueue.AddLast(new LinkedListNode <IToken>(createToken(GHaskellLexer.VCCURLY, "VCCURLY", next)));
                        indentStack.Pop();
                        nestedLevel--;
                    }
                }
            }

            if (next != null && type == GHaskellLexer.OCURLY)
            {
                prevWasKeyWord = false;
            }

            if (next == null || Hidden == next.Channel || GHaskellLexer.NEWLINE == type)
            {
                return(next);
            }

            if (type == GHaskellLexer.IN)
            {
                processINToken(next);
            }

            if (type == Eof)
            {
                processEofToken(next);
            }

            pendingDent = true;
            tokenQueue.AddLast(new LinkedListNode <IToken>(next));

            return(tokenQueue.First.Value);
        }