abstract-syntax-tree

How to use PHP-Parser to get the global variables name and change it

醉酒当歌 提交于 2020-06-08 19:37:38
问题 I want to use PHP-Parser library to get the global method ( _POST, _GET, _REQUEST ) to get values in PHP. I'm using PHP-Parser where I want to check the node name if it equal to ( _POST, _GET, _REQUEST ). I'm still beginner in PHP-Parser and not figure out how to get these global variables. For example, if I have the following source code: code: <?php $firstname = $_POST['firstname]; The PHP-parser I used until now looks like this: <?php require_once('vendor/autoload.php'); use PhpParser

LL top-down parser, from CST to AST

你离开我真会死。 提交于 2020-05-28 08:43:57
问题 I am currently learning about syntax analysis, and more especially, top-down parsing. I know the terminology and the difference with bottom-up LR parsers, and since top-down LL parsers are easier to implement by hand, I am looking forward to make my own. I have seen two kinds of approach: The recursive-descent one using a collection of recursive functions. The stack-based and table-driven automaton as shown here on Wikipedia. I am more interested by the latter, for its power and its

LL top-down parser, from CST to AST

房东的猫 提交于 2020-05-28 08:43:35
问题 I am currently learning about syntax analysis, and more especially, top-down parsing. I know the terminology and the difference with bottom-up LR parsers, and since top-down LL parsers are easier to implement by hand, I am looking forward to make my own. I have seen two kinds of approach: The recursive-descent one using a collection of recursive functions. The stack-based and table-driven automaton as shown here on Wikipedia. I am more interested by the latter, for its power and its

Find kind of TypeReference using TypeScript API

孤街浪徒 提交于 2020-05-26 06:11:09
问题 I'm trying to find the kind (class, interface, type alias, enumeration ...) of a TypeReference . I have this: const anode = node as ts.TypeReferenceNode; const symbol = this.typechecker.getSymbolAtLocation(anode.typeName) as ts.Symbol; const type = this.typechecker.getTypeOfSymbolAtLocation(symbol, anode); const decls = symbol.getDeclarations() as ts.Declaration[]; But the call to getSymbolAtLocation returns undefined . anode is a TypeReferenceNode (kind 159) according to VSC debugger: And

How to replace C# keywords with string subsitutes using Roslyn?

你离开我真会死。 提交于 2020-05-09 04:50:28
问题 I'd like to use Roslyn to load C# sources and write it to another file, replacing keywords with substitutes. Sample: for (int i=0; i<10; i++){} translated to foobar (int i=0; i<10; i++){} What the syntax for such operation could look like? 回答1: I don't know how well is this going to work, but you can replace each ForKeyword token with another ForKeyword token, but this time with your custom text. To do that, you can use CSharpSyntaxRewriter : class KeywordRewriter : CSharpSyntaxRewriter {

When to use ExtSlice node in Python's AST?

僤鯓⒐⒋嵵緔 提交于 2020-04-11 06:40:10
问题 Green Tree Snakes gives an example of using ExtSlice : >>> parseprint("l[1:2, 3]") Module(body=[ Expr(value=Subscript(value=Name(id='l', ctx=Load()), slice=ExtSlice(dims=[ Slice(lower=Num(n=1), upper=Num(n=2), step=None), Index(value=Num(n=3)), ]), ctx=Load())), ]) However this syntax won't work in interactive python shell: >>> foo = range(10) >>> foo[1:2,3] Traceback (most recent call last): File "<stdin>", line 1, in <module> TypeError: list indices must be integers, not tuple Anyone got an

When to use ExtSlice node in Python's AST?

£可爱£侵袭症+ 提交于 2020-04-11 06:40:07
问题 Green Tree Snakes gives an example of using ExtSlice : >>> parseprint("l[1:2, 3]") Module(body=[ Expr(value=Subscript(value=Name(id='l', ctx=Load()), slice=ExtSlice(dims=[ Slice(lower=Num(n=1), upper=Num(n=2), step=None), Index(value=Num(n=3)), ]), ctx=Load())), ]) However this syntax won't work in interactive python shell: >>> foo = range(10) >>> foo[1:2,3] Traceback (most recent call last): File "<stdin>", line 1, in <module> TypeError: list indices must be integers, not tuple Anyone got an

Run through the AST of an expression in sympy

别等时光非礼了梦想. 提交于 2020-03-26 03:50:45
问题 I'm using sympy to compute some higher order derivatives of a complicated function expression. I'd like to traverse the AST of the expression, e.g. go through the nodes depth first. How do I do that ? 回答1: A simple depth-first travel would be like this: from sympy import pi, sin from sympy.abc import a,x,y def depth_first_traverse(expr): for arg in expr.args: depth_first_traverse(arg, depth+1, new_marks+'+---', parent_ind=ind) if len(expr.args) == 0: # we reached a leaf of the tree pass # do

get ast from python object in interpreter

丶灬走出姿态 提交于 2020-03-23 08:15:22
问题 I am building an application for which I would like a naive user to define a simple function. I would then like to take this function and convert it into an abstract syntax tree. This should also work during an interactive session (i.e using the interpreter). Here's what I have tried so far in the interpreter: dill.source.getsource method inspect.getsource method accessing the function object's __code__ attribute The first two methods are problematic since they need for the function object to

AST matcher on a specific node

邮差的信 提交于 2020-03-04 05:03:22
问题 I wrote a AST matcher for finding specific type statements. In the matched nodes I calculated the neighbor siblings of that node. Now I need to run matcher on the neighbor nodes to verify they satisfies my condition or not. The clang AST matcher matches the whole tree node one by one. I want to run matcher against a particular node and return true if the node matches my required condition. Is this possible? 回答1: I suggest to implement your own matcher that will encapsulate the logic of