Exemplo n.º 1
0
 protected string PrintTokenPaths(IList <int[]> paths)
 {
     return(paths.Join("\n", arr => {
         Context.Entry e = null;
         Token t = GetTokenAt(tokens, arr, ref e);
         return arr.Join(", ") + ":" + t + " @" + ParseError.FilePositionOf(t, rows);
     }));
 }
Exemplo n.º 2
0
        public string FilePositionOf(Token token)
        {
            List <Context.Entry> traversed = new List <Context.Entry>();

            while (!token.IsValid)
            {
                Context.Entry e = token.GetAsContextEntry();
                if (e == null || traversed.IndexOf(e) >= 0)
                {
                    return("???");
                }
                traversed.Add(e);
                token = e.tokens[0];
            }
            return(ParseError.FilePositionOf(token, rows));
        }
Exemplo n.º 3
0
        public string FilePositionOf(Token token)
        {
            List <SyntaxTree> traversed = new List <SyntaxTree>();

            while (!token.IsValid)
            {
                SyntaxTree e = token.GetAsSyntaxNode();
                if (e == null || traversed.IndexOf(e) >= 0)
                {
                    return("???");
                }
                traversed.Add(e);
                token = e.tokens[0];
            }
            return(ParseError.FilePositionOf(token, rows));
        }
Exemplo n.º 4
0
        public static string Format(string format, List <object> args, object scope, Tokenizer tok, int tIndex)
        {
            StringBuilder sb = new StringBuilder();
            int           index = 0, start, end, tokenId;

            do
            {
                end = FindEndOfNextToken(tok, tIndex, format, index, out start, out tokenId);
                if (end < 0)
                {
                    sb.Append(format.Substring(index));
                    index = format.Length;
                }
                else
                {
                    if (tokenId < 0 || tokenId >= args.Count)
                    {
                        ParseError err = tok.AddError(tIndex, "invalid format token index (limit " +
                                                      args.Count + ")" + (tokenId < 0 ? "" : (" " + tokenId.ToString()))); err.col += start;
                        sb.Append(format.Substring(index, end - index));
                    }
                    else
                    {
                        if (index != start)
                        {
                            sb.Append(format.Substring(index, start - index));
                        }
                        string str = format.Substring(start, end - start);
                        str = str.Replace("{" + tokenId, "{" + 0);
                        sb.AppendFormat(str, args[tokenId]);
                    }
                    index = end;
                }
            } while (index < format.Length);
            return(sb.ToString());
        }
Exemplo n.º 5
0
 public void AddError(ParseError error)
 {
     errors.Add(error);
 }
Exemplo n.º 6
0
 public ParseError AddError(int index, string message)
 {
     ParseError e = new ParseError(index, rows, message); errors.Add(e); return(e);
 }
Exemplo n.º 7
0
 public void FilePositionOf(Token token, out int row, out int col)
 {
     ParseError.FilePositionOf(token, rows, out row, out col);
 }
Exemplo n.º 8
0
        public static int FindEndOfNextToken(Tokenizer tok, int startI, string str, int index, out int started, out int tokenId)
        {
            started = -1;
            tokenId = -1;
            for (int i = index; i < str.Length; ++i)
            {
                char c = str[i];
                switch (c)
                {
                case '{':
                    if (i + 1 >= str.Length)
                    {
                        ParseError err = tok.AddError(startI + i, "unexpected end of format token" +
                                                      (tokenId < 0?"": (" " + tokenId.ToString())));
                        return(-1);
                    }
                    if (str[i + 1] != '{')
                    {
                        if (started >= 0)
                        {
                            ParseError err = tok.AddError(startI + i, "unexpected beginning of new format token" +
                                                          (tokenId < 0 ? "" : (" " + tokenId.ToString())));
                            return(-1);
                        }
                        else
                        {
                            started = i;
                            ParseResult pr = IntegerParse(str, i + 1);
                            if (pr.IsError)
                            {
                                pr.error.OffsetBy(startI + i, tok.rows);
                                tok.AddError(pr.error);
                            }
                            else
                            {
                                tokenId = (int)(long)pr.replacementValue;
                            }
                        }
                    }
                    else
                    {
                        ++i;
                    }
                    break;

                case '}':
                    if (started >= 0)
                    {
                        if (tokenId < 0)
                        {
                            ParseError err = tok.AddError(startI + i, "token missing leading base 10 integer index" +
                                                          (tokenId < 0 ? "" : (" " + tokenId.ToString())));
                        }
                        return(i + 1);
                    }
                    break;
                }
            }
            if (started >= 0)
            {
                ParseError err = tok.AddError(startI + started, "expected end of format token" +
                                              (tokenId < 0 ? "" : (" " + tokenId.ToString())));
            }
            return(-1);
        }
Exemplo n.º 9
0
 public ParseResult SetError(string errorMessage, int index, int row, int col)
 {
     error = new ParseError(index, row, col, errorMessage); return(this);
 }
Exemplo n.º 10
0
 public ParseResult(int length, object value, string err, int i, int r, int c)
 {
     lengthParsed = length; replacementValue = value; error = new ParseError(i, r, c, err);
 }
Exemplo n.º 11
0
 public static ParseError AddError(this ITokenErrLog self, int index, string message)
 {
     ParseError e = new ParseError(index, self.GetTextRows(), message); self.AddError(e); return(e);
 }