2

I'm trying to define a grammar for ninja build with xtext.

There are three tricky points that I can't answer.

  • Indentations by tab:

How to handle indentations. A rule in a ninja build file might have several variable definitions with preceding tab spacing (similar to make files). This becomes a problem when the language has SL comments, ignores white-spaces and does indentation by tabs (python, make,...)

cflags = -g
rule cc
  command = gcc $cflags -c $in -o $out
  • Cross referencing reserved set of variable names:

There exists a set of reserved variables. Auto-complete should be able to reference both the reserved and the user defined set of variables.

command = gcc $cflags -c $in -o $out
  • Autocompleting cross referenced variable names which aren't seperated with WS

org.eclipse.xtext.common.Terminals hides WS tokens. ID tokens are seperated by white spaces. But in ninja script (similar to make files) the parsing should be done with longest matching variable name.

some_var = some_value
command  = $some_var.h

Any ideas are appreciated. Thanks.

Aykut Kllic
  • 898
  • 9
  • 14

1 Answers1

1

Check out the Xtext 2.8.0 release: https://www.eclipse.org/Xtext/releasenotes.html

The Whitespace-Aware Languages section states:

Xtext 2.8 supports languages in which whitespace is used to specify the structure, e.g. using indentation to delimit code blocks as in Python. This is done through synthetic tokens defined in the grammar:

terminal BEGIN: 'synthetic:BEGIN';
terminal END: 'synthetic:END';

These tokens can be used like other terminals in grammar rules:

WhitespaceAwareBlock:
    BEGIN
        ...
    END;

The new example language Home Automation available in the Eclipse examples (File → New → Example → Xtext Examples) demonstrates this concept. It allows code like the following:

Rule 'Report error' when Heater.error then
    var String report
    do
        Thread.sleep(500)
        report = HeaterDiagnostic.readError
    while (report == null)
    println(report)

More details are found in the documentation.

Boris Brodski
  • 8,425
  • 4
  • 40
  • 55