[IronPython] Usage of IronPython 2 Parser

Dino Viehland dinov at microsoft.com
Wed Sep 10 22:59:23 CEST 2008

Here's how to do it for both beta 4 & beta 5 in Python:

import clr
from IronPython.Compiler import Parser

# beta 4
from Microsoft.Scripting.Hosting import HostingHelpers
from Microsoft.Scripting.Hosting import ScriptRuntime
from System.Scripting import ErrorSink
from Microsoft.Scripting.Compilers import CompilerContext
py = ScriptRuntime.Create().GetEngine('py')

# beta 5
#from Microsoft.Scripting import ErrorSink
#from Microsoft.Scripting.Runtime import CompilerContext
#from Microsoft.Scripting.Hosting.Providers import HostingHelpers
#from IronPython.Hosting import Python
#py = Python.CreateEngine() # beta 5 and beyond

src = HostingHelpers.GetSourceUnit(py.CreateScriptSourceFromString('print "hello"'))
pylc = HostingHelpers.GetLanguageContext(py)

p = Parser.CreateParser(CompilerContext(src, pylc.GetCompilerOptions(), ErrorSink.Default), pylc.Options)
ast = p.ParseFile(True)

print ast.Body

-----Original Message-----
From: users-bounces at lists.ironpython.com [mailto:users-bounces at lists.ironpython.com] On Behalf Of Christian Schmidt
Sent: Wednesday, September 10, 2008 7:59 AM
To: Discussion of IronPython
Subject: [IronPython] Usage of IronPython 2 Parser

Dear community,

please could anyone provide an example of how to create a parser in
IronPython 2.0 Beta 4. The examples I found are outdated (based on IP
2.0a3) and my own try ended in NullReferenceException (see my post on



Users mailing list
Users at lists.ironpython.com

More information about the Ironpython-users mailing list