Repository: mesonbuild/meson

Issue:
Ternary operator inside format() fails
```
x = true
message('Using feature XXX   @0@'.format(x ? 'yes' : 'no'))
```

produces

```
Traceback (most recent call last):
  File "/home/adrian/.local/lib/python3.5/site-packages/mesonbuild/mesonmain.py", line 353, in run
    app.generate()
  File "/home/adrian/.local/lib/python3.5/site-packages/mesonbuild/mesonmain.py", line 148, in generate
    self._generate(env)
  File "/home/adrian/.local/lib/python3.5/site-packages/mesonbuild/mesonmain.py", line 188, in _generate
    intr = interpreter.Interpreter(b, g)
  File "/home/adrian/.local/lib/python3.5/site-packages/mesonbuild/interpreter.py", line 1327, in __init__
    self.load_root_meson_file()
  File "/home/adrian/.local/lib/python3.5/site-packages/mesonbuild/interpreterbase.py", line 124, in load_root_meson_file
    self.ast = mparser.Parser(code, self.subdir).parse()
  File "/home/adrian/.local/lib/python3.5/site-packages/mesonbuild/mparser.py", line 443, in parse
    block = self.codeblock()
  File "/home/adrian/.local/lib/python3.5/site-packages/mesonbuild/mparser.py", line 679, in codeblock
    curline = self.line()
  File "/home/adrian/.local/lib/python3.5/site-packages/mesonbuild/mparser.py", line 673, in line
    return self.statement()
  File "/home/adrian/.local/lib/python3.5/site-packages/mesonbuild/mparser.py", line 448, in statement
    return self.e1()
  File "/home/adrian/.local/lib/python3.5/site-packages/mesonbuild/mparser.py", line 451, in e1
    left = self.e2()
  File "/home/adrian/.local/lib/python3.5/site-packages/mesonbuild/mparser.py", line 476, in e2
    left = self.e3()
  File "/home/adrian/.local/lib/python3.5/site-packages/mesonbuild/mparser.py", line 485, in e3
    left = self.e4()
  File "/home/adrian/.local/lib/python3.5/site-packages/mesonbuild/mparser.py", line 494, in e4
    left = self.e5()
  File "/home/adrian/.local/lib/python3.5/site-packages/mesonbuild/mparser.py", line 501, in e5
    return self.e5add()
  File "/home/adrian/.local/lib/python3.5/site-packages/mesonbuild/mparser.py", line 504, in e5add
    left = self.e5sub()
  File "/home/adrian/.local/lib/python3.5/site-packages/mesonbuild/mparser.py", line 510, in e5sub
    left = self.e5mod()
  File "/home/adrian/.local/lib/python3.5/site-packages/mesonbuild/mparser.py", line 516, in e5mod
    left = self.e5mul()
  File "/home/adrian/.local/lib/python3.5/site-packages/mesonbuild/mparser.py", line 522, in e5mul
    left = self.e5div()
  File "/home/adrian/.local/lib/python3.5/site-packages/mesonbuild/mparser.py", line 528, in e5div
    left = self.e6()
  File "/home/adrian/.local/lib/python3.5/site-packages/mesonbuild/mparser.py", line 538, in e6
    return self.e7()
  File "/home/adrian/.local/lib/python3.5/site-packages/mesonbuild/mparser.py", line 544, in e7
    args = self.args()
  File "/home/adrian/.local/lib/python3.5/site-packages/mesonbuild/mparser.py", line 589, in args
    s = self.statement()
  File "/home/adrian/.local/lib/python3.5/site-packages/mesonbuild/mparser.py", line 448, in statement
    return self.e1()
  File "/home/adrian/.local/lib/python3.5/site-packages/mesonbuild/mparser.py", line 451, in e1
    left = self.e2()
  File "/home/adrian/.local/lib/python3.5/site-packages/mesonbuild/mparser.py", line 476, in e2
    left = self.e3()
  File "/home/adrian/.local/lib/python3.5/site-packages/mesonbuild/mparser.py", line 485, in e3
    left = self.e4()
  File "/home/adrian/.local/lib/python3.5/site-packages/mesonbuild/mparser.py", line 494, in e4
    left = self.e5()
  File "/home/adrian/.local/lib/python3.5/site-packages/mesonbuild/mparser.py", line 501, in e5
    return self.e5add()
  File "/home/adrian/.local/lib/python3.5/site-packages/mesonbuild/mparser.py", line 504, in e5add
    left = self.e5sub()
  File "/home/adrian/.local/lib/python3.5/site-packages/mesonbuild/mparser.py", line 510, in e5sub
    left = self.e5mod()
  File "/home/adrian/.local/lib/python3.5/site-packages/mesonbuild/mparser.py", line 516, in e5mod
    left = self.e5mul()
  File "/home/adrian/.local/lib/python3.5/site-packages/mesonbuild/mparser.py", line 522, in e5mul
    left = self.e5div()
  File "/home/adrian/.local/lib/python3.5/site-packages/mesonbuild/mparser.py", line 528, in e5div
    left = self.e6()
  File "/home/adrian/.local/lib/python3.5/site-packages/mesonbuild/mparser.py", line 538, in e6
    return self.e7()
  File "/home/adrian/.local/lib/python3.5/site-packages/mesonbuild/mparser.py", line 555, in e7
    left = self.method_call(left)
  File "/home/adrian/.local/lib/python3.5/site-packages/mesonbuild/mparser.py", line 618, in method_call
    args = self.args()
  File "/home/adrian/.local/lib/python3.5/site-packages/mesonbuild/mparser.py", line 590, in args
    a = ArgumentNode(s)
  File "/home/adrian/.local/lib/python3.5/site-packages/mesonbuild/mparser.py", line 351, in __init__
    self.subdir = token.subdir
AttributeError: 'TernaryNode' object has no attribute 'subdir'
```

Adding extra parenthesis around the ternary doesn't make any difference.



Discussion:

Resolving patch:
diff --git a/mesonbuild/mparser.py b/mesonbuild/mparser.py
--- a/mesonbuild/mparser.py
+++ b/mesonbuild/mparser.py
@@ -405,7 +405,8 @@ def __init__(self, lineno, colno, condition, block):
         self.block = block
 
 class TernaryNode(BaseNode):
-    def __init__(self, lineno, colno, condition, trueblock, falseblock):
+    def __init__(self, subdir, lineno, colno, condition, trueblock, falseblock):
+        self.subdir = subdir
         self.lineno = lineno
         self.colno = colno
         self.condition = condition
@@ -540,7 +541,7 @@ def e1(self):
             self.expect('colon')
             falseblock = self.e1()
             self.in_ternary = False
-            return TernaryNode(left.lineno, left.colno, left, trueblock, falseblock)
+            return TernaryNode(left.subdir, left.lineno, left.colno, left, trueblock, falseblock)
         return left
 
     def e2(self):


Test patch:
diff --git a/test cases/common/113 ternary/meson.build b/test cases/common/113 ternary/meson.build
--- a/test cases/common/113 ternary/meson.build	
+++ b/test cases/common/113 ternary/meson.build	
@@ -1,7 +1,12 @@
 project('ternary operator', 'c')
 
+x = true
 one = true ? 1 : error('False branch should not be evaluated')
 two = false ? error('True branch should not be evaluated.') : 2
+three = '@0@'.format(x ? 'yes' : 'no')
+four = [x ? '0' : '1']
 
 assert(one == 1, 'Return value from ternary true is wrong.')
 assert(two == 2, 'Return value from ternary false is wrong.')
+assert(three == 'yes', 'Return value for ternary inside method call is wrong.')
+assert(four == ['0'], 'Return value for ternary inside of list is wrong.')