Parsing JavaScript code in Java to get variable names

I have a Java String that contains javascript code, and I need to extract all javascript var s names.

So for the following javasctipt:

 var x; var a,b,c,d; var y = "wow"; var z = y + 'x'; 

I need to get "x,a,b,c,d,y,z" .

I do not need to get their values, just their names.

+4
source share
3 answers

Well, you can try and get the bindings that the script creates:

 ScriptEngineManager mgr = new ScriptEngineManager(); ScriptEngine se = mgr.getEngineByName("JavaScript"); try { se.eval("var x;var a,b,c,d;var y = \"wow\";var z = y+'x';"); Bindings bindings = se.getBindings(ScriptContext.ENGINE_SCOPE); System.out.println(bindings.keySet()); } catch (ScriptException e) { //e.printStackTrace(); } 

this prints [d, b, c, println, a, context, z, y, print, x]

as you can see, some additional bindings are defined: context, print and println

and here we filter them

 Set<String> keySet = bindings.keySet(); keySet.removeAll(Arrays.asList("context", "print", "println")); System.out.println(keySet); 

it prints [d, b, c, a, z, y, x]

+4
source

Something like the following:

 List<String> vars = new ArrayList<String>(); Pattern p = Pattern.compile("^\\s*var\\s+(.*?)\\s*;?$"); BifferedReader reader = .... // create the reader String line = null; while ((line = reader.readLine()) != null) { Matcher m = p.matcher(line); if (m.find()) { vars.addAll(Arrays.asList(m.group(1).split("\\s*,\\s*"))); } } 

Please note that I wrote this code without an IDE and never compiled or run it. Therefore, I'm sorry for the possible mistakes, but I'm sure that it is readable enough and can be a good starting point for you.

+1
source

I'm not sure if this would be too confusing, but I would probably get BNF for Javascript (see SO: BNF grammar repository? ) And then use ANTLR to create a parser for Javascript, which can then be used to extract all variables based on Javascript source code. See Also SO: ANTLR Tutorials .

0
source

Source: https://habr.com/ru/post/1381471/


All Articles