Skip to content

Grammar multiline support

kvaellning edited this page Jan 22, 2017 · 1 revision

Multiline support

Overall

It appears that creating a grammar for Scilab is rather difficult.

If one wants to test, just play around with the continuation mark (..). Using the continuation mark, the interpreter will glue two lines together, like nothing is happened in between (comments are ignored).

There arises the problem on "howto define a function/variable/etc." in a relativly fixed grammar (since continuation marks can only be resolved during runtime, or you will end up with a grammar which has a painfully amount begin/end-patterns).

I have tried my best to make the multi-line code capabilities as bulletproof as possible, but even then the continuation mark will mess up the code.

For the implementation, that means:

  1. Implement two version:
    first for one line, second for multiple lines.
  2. Be as generic as possible.
    There is no need for fancy stuff to identifiy the last bit.
  3. Get the elements right - and only describe them.

Assignment of multiple values

It was my initial idea to also rewrite all assignments and include multiline return values. Appears, this is not as easy as thought.

Scilab return values uses bracktes ([]) to start matrix/vector definitions, as well as defining return values of functions or doing multiple assignments at the same time.

This might appear "natural", but please bear in mind that this will make it impossible to determine what you expect:

Simple one-liner examples, which are plain easy:

  • [my, vec];
  • [my, vec] = SomeFunc();

Multi line examples:

  // 1. Example:
  foo = [my, ...
         vec];
  // 2. Example:
  [my, ..
   vec] = Foo()

Clear to see that here a begin / end pattern is needed. Now bear in mind that the Atom language parser works that way:

  1. Start at everything which is stated as begin
  2. End if you find a matching end or at EOF Since defining a vector and expecting multiple return values, that means that either one will win.

Now also keep in mind that all matrices are rectangular shaped, e.g. [1:10;1:10] denotes a 10x2 matrix. You cannot say this for return values: [foo,bar] = Foo() where foo might be a matrix, struct etc. and bar is another type and/or of another dimenstion.

That means:
Since both share the same pattern, which is not distinguishable by their beginning, multiline return values are not supported.