Skip to content

Instantly share code, notes, and snippets.

@jsyeo
Last active December 23, 2020 03:10
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jsyeo/aeee131bbdd60b090d27eec88529c85b to your computer and use it in GitHub Desktop.
Save jsyeo/aeee131bbdd60b090d27eec88529c85b to your computer and use it in GitHub Desktop.
JRuby AST from string
def parse s
sc = org.jruby.runtime.scope.ManyVarsDynamicScope.new(JRuby.runtime.getStaticScopeFactory().newLocalScope(nil, 'file'), nil)
is = java.io.ByteArrayInputStream.new(java.lang.String.new(s).get_bytes(java.nio.charset.StandardCharsets::UTF_8))
JRuby.runtime.parseFileFromMain is, 'file', sc
end
node = parse '1+3' #=> #<Java::OrgJrubyAst::RootNode:0x70f822e>
node.child_nodes.get(0)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment