Exemplo n.º 1
0
 public void Unescape()
 {
     Assert.AreEqual("abc", TextEscape.Unescape("abc"));
     Assert.AreEqual("\"", TextEscape.Unescape("\\\""));
     Assert.AreEqual("\n", TextEscape.Unescape("\\n"));
     Assert.AreEqual("\\", TextEscape.Unescape("\\\\"));
 }
Exemplo n.º 2
0
        public override ParseTree Parse(Lexer lexer, ParserState state)
        {
            lexer.Whitespace(state.RuntimeState);

            int start = lexer.Position;

            state.RuntimeState.Runtime.ParseTrace.Enter(this, lexer.CurrentSource(), range.ToString());

            char character = lexer.Peek();

            if (!range.Contains(character))
            {
                lexer.ErrorStrings[start].Add(range.ToString());

                state.RuntimeState.Runtime.ParseTrace.No(this,
                                                         lexer.SourceFrom(start), TextEscape.Quote(character));

                return(ParseTree.No);
            }

            lexer.Read();

            state.RuntimeState.Runtime.ParseTrace.Yes(this,
                                                      lexer.SourceFrom(start), TextEscape.Quote(character));

            if (state.BuildTextNodes)
            {
                return(new ParseTree(Convert.ToString(character)));
            }
            else
            {
                return(ParseTree.Yes);
            }
        }
Exemplo n.º 3
0
        public override ParseTree Parse(Lexer lexer, ParserState state)
        {
            lexer.Whitespace(state.RuntimeState);

            int start = lexer.Position;

            state.RuntimeState.Runtime.ParseTrace.Enter(this, lexer.CurrentSource(), "[]");

            char character = lexer.Peek();

            if (character == '\0')
            {
                state.RuntimeState.Runtime.ParseTrace.No(this, lexer.SourceFrom(start), "End of source");
                return(ParseTree.No);
            }

            lexer.Read();

            state.RuntimeState.Runtime.ParseTrace.Yes(this, lexer.SourceFrom(start), TextEscape.Quote(character));

            if (state.BuildTextNodes)
            {
                return(new ParseTree(Convert.ToString(character)));
            }
            else
            {
                return(ParseTree.Yes);
            }
        }
Exemplo n.º 4
0
        public static object GetMember(object obj, string member,
                                       object[] parametersHint, bool throwIfUndefined)
        {
            if (obj is IScope)
            {
                // FIXME - shouldn't be using exceptions for this

                try
                {
                    return(((IScope)obj).GetName(member));
                }
                catch
                {
                }
            }

            MemberInfo memberInfo = FindMember(obj, member,
                                               parametersHint);

            if (memberInfo == null)
            {
                if (throwIfUndefined)
                {
                    throw new Exception("can't find getable member "
                                        + TextEscape.Quote(member) + " in "
                                        + TypeNames.GetName(obj.GetType()));
                }
                else
                {
                    return(null);
                }
            }

            if (memberInfo is MethodInfo)
            {
                return(new ClrObjectMethodBinding(obj, (MethodInfo)memberInfo));
            }
            else if (memberInfo is PropertyInfo)
            {
                return(((PropertyInfo)memberInfo).GetValue(obj, null));
            }
            else if (memberInfo is FieldInfo)
            {
                object fieldValue = ((FieldInfo)memberInfo).GetValue(obj);

                if (fieldValue is Method)
                {
                    fieldValue = new ObjectMethodBinding(obj,
                                                         (Method)fieldValue);
                }

                return(fieldValue);
            }
            else
            {
                throw new Exception("can't get "
                                    + TypeNames.GetName(memberInfo.GetType()));
            }
        }
Exemplo n.º 5
0
 public override string ToString()
 {
     if (min == max)
     {
         return(TextEscape.Quote(min));
     }
     else
     {
         return(TextEscape.Quote(min) + ".." + TextEscape.Quote(max));
     }
 }
Exemplo n.º 6
0
        public RuntimeObject Parse(Lexer lexer, RuntimeState runtimeState)
        {
            runtimeState.Runtime.ParseTrace.Enter(this, null, lexer.FileName);

            while (true)
            {
                ParserState state = new ParserState(runtimeState);

                ParseTree tree;

                if (runtimeState.Runtime.CompileParser)
                {
                    tree = RootPattern.ParseUsingCompiler(lexer, state);
                }
                else
                {
                    tree = RootPattern.Parse(lexer, state);
                }

                RuntimeObject obj = (RuntimeObject)tree.Value;

                if (!lexer.Finished)
                {
                    string got;

                    if (lexer.MaxPosition < lexer.Text.Length)
                    {
                        got = TextEscape.Quote(lexer.Text[lexer.MaxPosition]);
                    }
                    else
                    {
                        got = "end of source code";
                    }

                    throw new ParseException(
                              lexer.SourceAt(lexer.MaxPosition),
                              lexer.ErrorStrings[lexer.MaxPosition],
                              got);
                }

                runtimeState.Runtime.ParseTrace.Parsed(lexer.FileName, obj);

                if (lexer.ChangingRoots)
                {
                    lexer.Restart();
                    continue;
                }

                runtimeState.Runtime.ParseTrace.Yes(this, null);

                return(obj);
            }
        }
Exemplo n.º 7
0
        public override ParseGraphNode BuildParseGraph(RuntimeState state)
        {
            string text = (string)((String)this.text).text;

            text = TextEscape.Unquote(text);

            if (text.Length == 1)
            {
                return(new CharNode(Source, text[0]));
            }
            else
            {
                return(new TextNode(Source, text));
            }
        }
Exemplo n.º 8
0
        public override ParseGraphNode BuildParseGraph(RuntimeState state)
        {
            string min = (string)((Base.String) this.min).text;
            string max = (string)((Base.String) this.max).text;

            min = TextEscape.Unquote(min);
            max = TextEscape.Unquote(max);

            if ((min.Length != 1) || (max.Length != 1))
            {
                throw new Exception();
            }

            CharRange range = new CharRange(min[0], max[0]);

            return(new CharNode(Source, range));
        }
Exemplo n.º 9
0
 public override string ToString()
 {
     if (parameter == null)
     {
         return("    " + op.ToString());
     }
     else if (parameter is string)
     {
         return("    " + op + " " + TextEscape.Quote((string)parameter));
     }
     else if (parameter is Type)
     {
         return("    " + op + " " + TypeNames.GetName((Type)parameter));
     }
     else if (parameter is FieldInfo)
     {
         FieldInfo field = (FieldInfo)parameter;
         return("    " + op + " " + TypeNames.GetName(field.DeclaringType) + "::" + field.Name);
     }
     else if (parameter is MethodInfo)
     {
         MethodInfo method = (MethodInfo)parameter;
         return("    " + op + " " + TypeNames.GetName(method.DeclaringType) + "::" + method.Name);
     }
     else if (parameter is ConstructorInfo)
     {
         ConstructorInfo constructor = (ConstructorInfo)parameter;
         return("    " + op + " " + TypeNames.GetName(constructor.DeclaringType));
     }
     else if (parameter is BlockLocal)
     {
         return("    " + op + " " + TypeNames.GetName(((BlockLocal)parameter).Type));
     }
     else
     {
         return("    " + op + " " + parameter);
     }
 }
Exemplo n.º 10
0
        private ParseGraphNode ParseNode()
        {
            string token = Read();

            if ((token == "s") || (token == "a"))
            {
                Read("(");

                List <ParseGraphNode> nodes = new List <ParseGraphNode>();

                while (Peek() != ")")
                {
                    nodes.Add(ParseNode());
                }

                Read(")");

                if (token == "s")
                {
                    return(new SeqNode(null, nodes));
                }
                else if (token == "a")
                {
                    return(new AltNode(null, nodes, false));
                }
            }
            else if ((token == "?") || (token == "*") || (token == "+"))
            {
                Read("(");
                ParseGraphNode body = ParseNode();
                Read(")");

                return(new RepNode(null, body, Reps.ForName(token[0])));
            }
            else if (token == "&")
            {
                Read("(");
                ParseGraphNode body = ParseNode();
                Read(")");

                return(new AndNode(null, body));
            }
            else if (token == "!")
            {
                Read("(");
                ParseGraphNode body = ParseNode();
                Read(")");

                return(new NotNode(null, body));
            }
            else if (token == "r")
            {
                Read("(");

                char min = TextEscape.Unquote(Read())[0];
                char max = TextEscape.Unquote(Read())[0];

                Read(")");

                return(new CharNode(null, new CharRange(min, max)));
            }
            else if (token == "l")
            {
                Read("(");

                string         label = Read();
                ParseGraphNode body  = ParseNode();

                Read(")");

                return(new LabelNode(null, label, body));
            }
            else if (token == "t")
            {
                Read("(");

                ParseGraphNode body = ParseNode();

                Read(")");

                return(new TokenNode(null, body));
            }
            else if (token == "o")
            {
                Read("(");

                Dictionary <string, object> options = new Dictionary <string, object>();

                while (Peek() == "(")
                {
                    Read();

                    string optionName  = Read();
                    object optionValue = ReadValue();

                    options[optionName] = optionValue;

                    Read(")");
                }

                ParseGraphNode body = ParseNode();

                Read(")");

                OptionsNode optionsNode = new OptionsNode(null, body);

                foreach (string key in options.Keys)
                {
                    switch (key)
                    {
                    case "buildTextNodes":
                        optionsNode.BuildTextNodes.Value = (bool)options[key];
                        break;

                    case "dropPrecedence":
                        optionsNode.DropPrecedence.Value = (bool)options[key];
                        break;

                    case "recursive":
                    {
                        if ((bool)options[key])
                        {
                            optionsNode.RecursionBehaviour.Value = RecursionBehaviour.Recursive;
                        }
                        else
                        {
                            optionsNode.RecursionBehaviour.Value = RecursionBehaviour.None;
                        }
                    } break;

                    case "leftRecursive":
                        optionsNode.RecursionBehaviour.Value = RecursionBehaviour.LeftRecursive;
                        break;

                    case "rightRecursive":
                        optionsNode.RecursionBehaviour.Value = RecursionBehaviour.RightRecursive;
                        break;

                    case "whitespace":
                    {
                        object value = options[key];

                        if (value == null)
                        {
                            optionsNode.Whitespace.Value = null;
                        }
                        else
                        {
                            optionsNode.Whitespace.Value = Pattern.PatternForType((Type)value);
                        }
                    } break;

                    case "exclude":
                        optionsNode.Exclude.Value = Pattern.PatternForType((Type)options[key]);
                        break;
                    }
                }

                return(optionsNode);
            }
            else if (token == "any")
            {
                return(new AnyNode(null));
            }
            else if ((token[0] >= '0') && (token[0] <= '9'))
            {
                return(new PatternNode(null, patterns[Convert.ToInt32(token)], false));
            }
            else if (token[0] == '\'')
            {
                string text = TextEscape.Unquote(token);

                if (text.Length == 1)
                {
                    return(new CharNode(null, text[0]));
                }
                else
                {
                    return(new TextNode(null, text));
                }
            }

            throw new Exception(token);
        }
Exemplo n.º 11
0
        public override Block CompileNewState(Runtime runtime,
                                              StateForCompiler state)
        {
            ParserBlock block = new ParserBlock(runtime);

            block.Comment("start of text --------------------");

            block.Enter(this, TextEscape.Quote(text));

            block.BeginScope();

            block.Whitespace(runtime, state);
            BlockLocal start = block.SavePosition();

            BlockLocal n = new BlockLocal(typeof(int));

            block.DeclareLocal(n);

            BlockLabel reitterate = new BlockLabel("reitterate");

            block.MarkLabel(reitterate);

            block.Comment("itteration");

            BlockLocal character = new BlockLocal(typeof(char));

            block.DeclareLocal(character);

            block.LoadLexer();
            block.LoadLocal(n);
            block.Call(typeof(Lexer).GetMethod("Peek",
                                               new Type[] { typeof(int) }));

            block.Dup();
            block.StoreLocal(character);

            block.Load(text);
            block.LoadLocal(n);
            block.GetProperty(typeof(string).GetProperty("Chars"));

            BlockLabel matched = new BlockLabel("matched");

            block.BranchIfEqual(matched);

            block.Comment("handle the failure");

            // todo error string

            // todo specifics
            block.No(this, start);

            block.LoadNo();

            BlockLabel returnLabel = new BlockLabel("return");

            block.Branch(returnLabel);

            block.MarkLabel(matched);

            block.Comment("increment and check the loop variable");

            block.LoadLocal(n);
            block.Increment();
            block.Dup();

            block.StoreLocal(n);

            block.Load(text.Length);
            block.BranchIfLess(reitterate);

            block.Comment("skip over the text");

            block.LoadLexer();
            block.Load(text.Length);
            block.Call(typeof(Lexer).GetMethod("Skip"));

            // todo specifics
            block.Yes(this, start);

            block.Comment("create the parse tree");

            if (state.BuildTextNodes)
            {
                block.Load(text);
                block.New(typeof(ParseTree).GetConstructor(
                              new Type[] { typeof(object) }));
            }
            else
            {
                block.LoadYes();
            }

            block.EndScope();

            block.MarkLabel(returnLabel);

            block.Comment("end of text --------------------");

            return(block);
        }
Exemplo n.º 12
0
        public override ParseTree Parse(Lexer lexer, ParserState state)
        {
            lexer.Whitespace(state.RuntimeState);

            int start = lexer.Position;

            state.RuntimeState.Runtime.ParseTrace.Enter(
                this,
                lexer.CurrentSource(),
                TextEscape.Quote(text));

            for (int n = 0; n < text.Length; n++)
            {
                char character = lexer.Peek(n);

                if (character != text[n])
                {
                    string stoppedString;

                    if (character == '\0')
                    {
                        stoppedString = "end";
                    }
                    else
                    {
                        stoppedString = TextEscape.Quote(character);
                    }

                    lexer.ErrorStrings[start + n].Add(TextEscape.Quote(text));

                    if (n > 0)
                    {
                        state.RuntimeState.Runtime.ParseTrace.No(
                            this,
                            lexer.SourceFrom(start),
                            TextEscape.Quote(lexer.Text.Substring(start, n))
                            + "..." + stoppedString);
                    }
                    else
                    {
                        state.RuntimeState.Runtime.ParseTrace.No(
                            this,
                            lexer.SourceFrom(start),
                            stoppedString);
                    }

                    return(ParseTree.No);
                }
            }

            lexer.Skip(text.Length);

            state.RuntimeState.Runtime.ParseTrace.Yes(this,
                                                      lexer.SourceFrom(start));

            if (state.BuildTextNodes)
            {
                return(new ParseTree(text));
            }
            else
            {
                return(ParseTree.Yes);
            }
        }
Exemplo n.º 13
0
 public ObjectViewHeader GetHeader(object obj)
 {
     return(new ObjectViewHeader(TextEscape.Quote((string)obj), false, false));
 }
Exemplo n.º 14
0
 private void AddNode(TreeIter parent, TextNode textNode)
 {
     store.AppendValues(parent, TextEscape.Quote(textNode.Text), textNode);
 }
Exemplo n.º 15
0
 private void Add(TreeIter parent, string prefix, string node)
 {
     store.AppendValues(parent, prefix + TextEscape.Quote(node), GtkProofBox.Box(node));
 }