[IronPython] Usage of Parser.FromString

Dino Viehland dinov at exchange.microsoft.com
Mon Aug 14 17:31:50 CEST 2006

The easiest way is to derive a new class from AstWalker or AstWalkerNonRecursive, override the nodes you care about (or all of them, or implement the interface on your own class), and then do the stmt.Walk(walker) as you have.  For example:

class MyWalker : AstWalker {
        public override bool Walk(ExpressionStatement exprStmt) {
                TreeNode node = new TreeNode();
                node.Tag = exprStmt;
                return false;

Do that for every node type and you'll add all the top level nodes (the false prevents us from walking the node further which would add lower level nodes into the top-level).  When the user expands a node you can walk the Tag and then fill in the tree view (or you can return true and fill the entire tree view at once by maintaining where you are in the hierarchy).

-----Original Message-----
From: users-bounces at lists.ironpython.com [mailto:users-bounces at lists.ironpython.com] On Behalf Of Christian Schmidt
Sent: Monday, August 14, 2006 6:32 AM
To: users at lists.ironpython.com
Subject: [IronPython] Usage of Parser.FromString

I'm trying to fill a treeview with the statements found in a embedded python script. How do I traverse through the tree of parsed statements?

using IronPython;
using IronPython.Compiler;
using IronPython.Compiler.Ast;

void doparse(string txt) {
   SystemState ss = new SystemState(engineOptions);
   CompilerContext cc = new CompilerContext();
   Parser parser = Parser.FromString (ss, cc, txt);
   Statement stmt = parser.ParseFileInput();
   IAstWalker walker = MyWalker();

users mailing list
users at lists.ironpython.com

More information about the Ironpython-users mailing list