javalang icon indicating copy to clipboard operation
javalang copied to clipboard

Unary Operators?

Open new32 opened this issue 8 years ago • 1 comments

javalang.tokenize.tokenize correctly identifies the unary operators within the token list however, javalang.parse.parse does not contain any nodes for these.

Using this example:

public class JavaTest
  extends Object
{
  public static void main(String[] args)
  {
    int counter = 0;
    counter++;
    boolean flag = false;
    if(!flag && counter)
    {
      System.out.println("Stuff!");
    }
  }
}

with this script:

import sys
import javalang

class Example:
  def __init__(self, infile):
    self.contents = ""
    with open(infile, "r") as FIN:
      for line in FIN:
        self.contents += line
    if self.contents:
      print("Tokens:")
      tokens = list(javalang.tokenizer.tokenize(self.contents))
      for token in tokens:
        print(token)
      print("Nodes:")
      tree = javalang.parse.parse(self.contents)
      for path, node in tree:
        my_str = str(node)
        if hasattr(node, "position"):
          if node.position:
            my_str += " " + str(node.position)
        if hasattr(node, "value"):
          if node.value:
            my_str += " " + str(node.value)
        if isinstance(node, javalang.tree.IfStatement):
          my_str += "\n--condition: " + str(node.condition)
          if isinstance(node.condition, javalang.tree.BinaryOperation):
            my_str += "\n----operator: " + str(node.condition.operator)
            my_str += "\n----Left:  " + str(node.condition.operandl.member)
            my_str += "\n----Right: " + str(node.condition.operandr.member)
        if isinstance(node, javalang.tree.BinaryOperation):
          my_str += "\n--operator: " + str(node.operator)
        print(my_str)

if __name__ == "__main__":
  if len(sys.argv) > 1:
    for name in sys.argv[1:]:
      Example(name)

If you run these examples, the tokenizer correctly prints out the postfix increment and the not operators. When breaking down the nodes though, the increment and not are left out entirely. Of special interest is the breakdown of the AND gate:

IfStatement
--condition: BinaryOperation
----operator: &&
----Left:  flag
----Right: counter

Popping through the Parser code, I'm not actually seeing anything that finds unary ops in the compilation_unit logic (admittedly, I've only been looking for a bit). Am I just missing something?

new32 avatar May 22 '17 02:05 new32

The set of prefix and postfix unary operators should be available on Primary expressions. You should be able to access these as prefix_operators and postfix_operators on operandl and operandr from your example.

c2nes avatar May 22 '17 17:05 c2nes