Hi Dino,
Thanks a lot. It's working reasonably well now. It would be nice to have more comprehensive documentation of these classes, as it is quite a hassle to extract the variables. I'm now extracting the assignment statements from the script, but excluding those that are inside function bodies. In the expression, I get al name expressions and then remove the function names (call expressions targets) and the global variables. Thanks a lot for your help. With best regards, Rutger Koperdraad _____ The ExchangeIt Support Team Description: cid:image001.jpg@01C8455E.47D3E2D0 EUR: O: +31 (0)20 893 2601 | F: +31 (0)172 413 746 USA: O: +1 (925) 208 4716 Support request only via our online support ticket system at <http://www.exchangeit24.eu/> www.ExchangeIt24.eu SALES: <mailto:sa...@exchangeit24.com> sa...@exchangeit24.com WEB: <http://www.exchangeit24.eu/> www.ExchangeIt24.eu FORUM: <http://www.cardexchange.eu/> www.CardExchange.eu ... Solving today's card production problems, with tomorrow in mind! IT'S HERE! CHECK OUT THE NEW CARDEXCHANGER 6! <http://www.exchangeit24.eu/index.php/products/cardexchange/cardexchangetria lversion.html> Click here to download a FREE trial version This email and any files transmitted with it are confidential and intended solely for the use of the individual or entity to whom they are addressed. If you have received this email in error please notify the system manager. This message contains confidential information and is intended only for the individual named. If you are not the named addressee you should not disseminate, distribute or copy this e-mail. Please notify the sender immediately by e-mail if you have received this e-mail by mistake and delete this e-mail from your system. If you are not the intended recipient you are notified that disclosing, copying, distributing or taking any action in reliance on the contents of this information is strictly prohibited. From: Dino Viehland [mailto:di...@microsoft.com] Sent: Thursday, 15 December, 2011 14:07 To: Rutger Koperdraad; ironpython-users@python.org Subject: RE: [Ironpython-users] PythonAst I'd suggest adding a method in the walker which handles AssignmentStatements and looks to see if the left-hand side is a NameExpression. If it is then you can add it to a list of excluded variable names which you can remove from clnVariableNames after the walk is finished. From: ironpython-users-bounces+dinov=microsoft....@python.org [mailto:ironpython-users-bounces+dinov=microsoft....@python.org] On Behalf Of Rutger Koperdraad Sent: Thursday, December 15, 2011 8:34 AM To: ironpython-users@python.org Subject: [Ironpython-users] PythonAst Hi, I'm writing an application in Visual Basic .NET that allows the users to write scripts in IronPython for some specialized customization needs. Users provide a script, a number of variable names and values, and an expression. For example, to customize the displaying of dates, they could provide the following script, variables and expressions: Script: dateformat = 'dddd d MMMM yyyy' prefix = 'Birth date: ' def format(d): import System dd = System.DateTime.Parse(d) return dd.ToString(dateformat) Variables: birthdate = '10/04/1968' Expression: prefix + format(birthdate) In Visual Basic .NET I create a ScriptRuntime, ScriptEngine, ScriptSource and ScriptScope. I add the variables to the ScriptScope, execute the script and evaluate the expression. That's working like a charm. I would like to add functionality that automatically detects which variables need to be defined. In the above example, I would like the user to specify the script and the expression and the software to detect that the variable "birthdate" is needed and prompt for a value. I tried to do so with PythonAst, but I cannot find sufficient documentation on internet to get it working in general. The functions below work to some extent. They get all the names from the expression and then remove "format" for being a function name and not a variable name. But this function still returns "prefix", which it shouldn't. Any help or documentation would be appreciated. Public Function GetVariableNames(ByVal strExpression As String) As StringCollection Try If Not String.IsNullOrEmpty(strExpression) Then ' Create a script runtime if needed If m_objScriptRuntime Is Nothing Then m_objScriptRuntime = ScriptRuntime.CreateFromConfiguration() End If ' Create the objects needed by the expression walker Dim objEngine As ScriptEngine = m_objScriptRuntime.GetEngine(ScriptLanguage) Dim objSource As ScriptSource = objEngine.CreateScriptSourceFromString(strExpression, SourceCodeKind.Expression) Dim objSourceUnit As SourceUnit = Providers.HostingHelpers.GetSourceUnit(objSource) Dim objLanguageContext As LanguageContext = HostingHelpers.GetLanguageContext(objEngine) Dim objCompilerContext As New CompilerContext(objSourceUnit, objLanguageContext.GetCompilerOptions(), ErrorSink.Default) Dim objParser As Parser = Parser.CreateParser(objCompilerContext, New PythonOptions) Dim objPythonAst As PythonAst = objParser.ParseSingleStatement() Dim objExpressionWalker As New ExpressionWalker ' Determine the variable names Call objPythonAst.Walk(objExpressionWalker) Return objExpressionWalker.VariableNames End If Catch ex As Exception Call LogException(strExpression, ex) End Try Return New StringCollection End Function Private Class ExpressionWalker Inherits PythonWalker Private clnVariableNames As New StringCollection Public ReadOnly Property VariableNames As StringCollection Get Return clnVariableNames End Get End Property Public Overrides Sub PostWalk(node As NameExpression) Call clnVariableNames.Add(node.Name) Call MyBase.PostWalk(node) End Sub Public Overrides Sub PostWalk(node As CallExpression) Dim objNameExpression As NameExpression = TryCast(node.Target, NameExpression) If Not objNameExpression Is Nothing Then Call clnVariableNames.Remove(objNameExpression.Name) End If Call MyBase.PostWalk(node) End Sub End Class With best regards, Rutger Koperdraad.
<<image001.jpg>>
_______________________________________________ Ironpython-users mailing list Ironpython-users@python.org http://mail.python.org/mailman/listinfo/ironpython-users