Ejemplo n.º 1
0
        public static SplitItem operator +(SplitItem a, SplitItem b)
        {
            var c = new SplitItem(a.text + b.text, a.position > b.position ? a.position : b.position,
                                  a.line > b.line ? a.line : b.line);

            return(c);
        }
Ejemplo n.º 2
0
        static void applyList(List <SplitItem> list)
        {
            Queue <SplitItem> tokens = new Queue <SplitItem>(list);

            list.Clear();
            SplitItem back = null;

            while (tokens.Count > 0)
            {
                SplitItem token = tokens.Dequeue();
                if (tokens.Count > 0)
                {
                    SplitItem next = tokens.Peek();
                    if (token == "=" && next == "=")
                    {
                        back = null;
                        tokens.Dequeue();
                        list.Add(new SplitItem("==", token.position, token.line));
                        continue;
                    }
                    if (token == "/" && next == "/")
                    {
                        back = null;
                        tokens.Dequeue();
                        list.Add(new SplitItem("//", token.position, token.line));
                        continue;
                    }
                    if (token == "/" && next == "*")
                    {
                        back = null;
                        tokens.Dequeue();
                        list.Add(new SplitItem("/*", token.position, token.line));
                        continue;
                    }
                    if (token == "*" && next == "/")
                    {
                        back = null;
                        tokens.Dequeue();
                        list.Add(new SplitItem("*/", token.position, token.line));
                        continue;
                    }
                    if (token == "!" && next == "=")
                    {
                        back = null;
                        tokens.Dequeue();
                        list.Add(new SplitItem("!=", token.position, token.line));
                        continue;
                    }
                    if (token == ">" && next == "=")
                    {
                        back = null;
                        tokens.Dequeue();
                        list.Add(new SplitItem(">=", token.position, token.line));
                        continue;
                    }
                    if (token == "<" && next == "=")
                    {
                        back = null;
                        tokens.Dequeue();
                        list.Add(new SplitItem("<=", token.position, token.line));
                        continue;
                    }
                    if (token == "-" && next == ">")
                    {
                        back = null;
                        tokens.Dequeue();
                        list.Add(new SplitItem("->", token.position, token.line));
                        continue;
                    }

                    if (token == "&" && next == "&")
                    {
                        back = null;
                        tokens.Dequeue();
                        list.Add(new SplitItem("&&", token.position, token.line));
                        continue;
                    }
                    if (token == "|" && next == "|")
                    {
                        back = null;
                        tokens.Dequeue();
                        list.Add(new SplitItem("||", token.position, token.line));
                        continue;
                    }
                    if (finalSplitProcessor != null)
                    {
                        FinalSplit final;
                        if (finalSplitProcessor(token, next, out final))
                        {
                            back = null;
                            tokens.Dequeue();
                            list.Add(new SplitItem(final.result, token.position, token.line));
                        }
                    }
                    /* Optional Negative Number Parser */
                    if (token == "-")
                    {
                        long num;
                        if (long.TryParse(next, out num))
                        {
                            back = null;
                            tokens.Dequeue();
                            list.Add(token + next);
                            continue;
                        }
                    }
                    if (token == "=" && next == ">")
                    {
                        back = null;
                        tokens.Dequeue();
                        list.Add(new SplitItem("=>", token.position, token.line));
                        continue;
                    }
                    /* Optional Dotted Number Parser */
                    if (token == ".")
                    {
                        long num;
                        if (long.TryParse(back, out num))
                        {
                            if (long.TryParse(next, out num))
                            {
                                tokens.Dequeue();
                                list.RemoveAt(list.Count - 1);
                                list.Add(back + token + next);
                                continue;
                            }
                        }
                    }
                    /* Optional Separated-By-Character ID */
                    if (AcceptSeparatedID)
                    {
                        if (token == "-" && back != "-" && next != "-" &&
                            Token.IsValidIdentifier(back) && Token.IsValidIdentifier(next))
                        {
                            tokens.Dequeue();
                            list.RemoveAt(list.Count - 1);
                            list.Add(back + token + next);
                            back = list[list.Count - 1];
                            continue;
                        }
                    }
                }
                back = token;
                list.Add(token);
            }
        }
Ejemplo n.º 3
0
        //public static implicit operator bool (Token token) => token != null;

        public static Token build(SplitItem item)
        {
            string text  = item.text;
            object value = null;
            var    type  = TokenType.None;

            //text = text.ToLower();
            switch (text)
            {
            //case "say":type = TokenType.Say;break;
            //case "echo":type = TokenType.Echo;break;
            //case "choice":type = TokenType.Choice;break;
            //case "chose":type = TokenType.Chose;break;
            //case "define":type = TokenType.Define;break;
            //case "const":type = TokenType.Constant;break;
            //case "set":type = TokenType.Set;break;
            //case "invoke":type = TokenType.Invoke;break;
            //case "input":type = TokenType.Input;break;
            //case "label":type = TokenType.Label;break;
            //case "goto":type = TokenType.Goto;break;
            //case "to":type = TokenType.To;break;

            case "@": type = TokenType.Email; break;

            case "//": type = TokenType.Comment; break;

            case "/*": type = TokenType.CommentBegin; break;

            case "*/": type = TokenType.CommentEnd; break;

            case "(": type = TokenType.OpenParenthesis; break;

            case ")": type = TokenType.CloseParenthesis; break;

            case "[": type = TokenType.OpenSquares; break;

            case "]": type = TokenType.CloseSquares; break;

            case "{": type = TokenType.OpenBrackets; break;

            case "}": type = TokenType.CloseBrackets; break;

            case "true":
                type  = TokenType.TrueLiteral;
                value = true;
                break;

            case "false":
                type  = TokenType.FalseLiteral;
                value = false;
                break;

            case "null": type = TokenType.NullLiteral; break;

            //case "null": type = TokenType.ID;break;
            case "+": type = TokenType.Plus; break;

            case "-": type = TokenType.Minus; break;

            case "/": type = TokenType.Divide; break;

            case "*": type = TokenType.Multiply; break;

            //case "if":type = TokenType.If;break;
            //case "else":type = TokenType.Else;break;
            //case "function":type = TokenType.Function;break;
            //case "global":type = TokenType.Field;break;
            //case "public":type = TokenType.Public;break;
            //case "private":type = TokenType.Private;break;
            //case "static":type = TokenType.Static;break;
            //case "internal":type = TokenType.Internal;break;
            //case "export":type = TokenType.Export;break;
            //case "type":type = TokenType.Type;break;
            //case "for":type = TokenType.For;break;
            //case "while":type = TokenType.While;break;
            //case "foreach":type = TokenType.ForEach;break;
            case "void": type = TokenType.VoidLiteral; break;

            //case "var":type = TokenType.Variable;break;
            case "=": type = TokenType.Bind; break;

            case "==": type = TokenType.Equal; break;

            case "!=": type = TokenType.Different; break;

            case ">": type = TokenType.Larger; break;

            case "<": type = TokenType.Lower; break;

            case ">=": type = TokenType.LargerOrEqual; break;

            case "<=": type = TokenType.LowerOrEqual; break;

            case "\n": type = LineBreakCountAsEOF ? TokenType.EOF : TokenType.LineBreak; break;

            case ";": type = TokenType.EOF; break;

            case ",": type = TokenType.Comma; break;

            case ":": type = TokenType.Descript; break;

            case "$": type = TokenType.Rich; break;

            case "&": type = TokenType.Reference; break;

            case "->": type = TokenType.Access; break;

            case "=>": type = TokenType.To; break;

            case "AND": type = TokenType.And; break;

            case "&&": type = TokenType.And; break;

            case "OR": type = TokenType.Or; break;

            case "||": type = TokenType.Or; break;

            case ".": type = TokenType.Dot; break;

            case "#": type = TokenType.Hashtag; break;

            case "!": type = TokenType.Exclamation; break;

                //case "return":type = TokenType.Return;break;
                //case "then":type = TokenType.Then;break;
                //case "end":type = TokenType.End;break;

                //case "loop":type = TokenType.Loop;break;
            }
            //switch(text.ToLower())
            //{
            //	case "scope":type = TokenType.Scope;break;
            //}
            if (type == TokenType.None)
            {
                if (text[0] == '"' && text[text.Length - 1] == '"')
                {
                    type  = TokenType.StringLiteral;
                    value = text.Remove(0, 1).Remove(text.Length - 2, 1);
                }
                else
                {
                    if (tryID(text))
                    {
                        type  = TokenType.ID;
                        value = text;
                        goto end;
                    }
                    int integer;
                    if (int.TryParse(text, out integer))
                    {
                        type  = TokenType.IntegerLiteral;
                        value = integer;
                        goto end;
                    }
                    float single;
                    if (text[text.Length - 1] == 'f')
                    {
                        if (float.TryParse(text.Replace('.', ',').Replace("f", ""), out single))
                        {
                            type  = TokenType.FloatLiteral;
                            value = single;
                            goto end;
                        }
                    }
                    if (float.TryParse(text.Replace('.', ','), out single))
                    {
                        type  = TokenType.FloatLiteral;
                        value = single;
                        goto end;
                    }
                    char c;
                    if (char.TryParse(text, out c))
                    {
                        type  = TokenType.CharLiteral;
                        value = c;
                    }
                }
            }
end:
            return(new Token(text, type, item.position, item.line, value));
        }