akaRem
akaRem

Reputation: 7638

Writing regular expressions and rules in Sublime Text 2 syntax definitions

I'm very interested in Syntax Definitions for Sblime text 2

I've studied the basics but I don't know how to write RE (and rules) for smth like variable = sentense, i.e. myvar = func(foo, bar) + baz

I can't write anything better than ^\s*([^=\n]+)=([^=\n]+\n) (that doesn't work) How to write this RE in proper way?

Also, i have some difficulties in defining RE for block

IF i FROM .. TO ..
    ...
ELSE
    ...
END IF

Hoe to write it?

Upvotes: 1

Views: 1027

Answers (1)

GPrimola
GPrimola

Reputation: 1695

In this case you have to write a parser. A regex won't work because the patterns may vary. You've already noticed it when you stated 'variable = sentence'. For this, you can use spoofax or javacup for grammar definitions. I'll give you a snip in JavaCup:

Scanner issues: suppose 'variable' follows the pattern: (_|[a-zA-Z])(_|[a-zA-Z])* and 'number' is: ([0-9])+ Note that number could be any decimal or int, but here I state it as that pattern, supposing my language only deals with integer (or whatever that pattern means :) ).

Now we can declare our grammar following the JavaCUP syntax. Which is more or less like:

expression ::= variable "=" sentence

sentence ::= sentence "+" sentence;
sentence ::= sentence "-" sentence;
sentence ::= sentence "*" sentence;
sentence ::= sentence "/" sentence;

sentence ::= number;

...and that goes further.

If you've never had any compiler's class, it may seems very difficult to see. Plus there is a lot of grammar's restrictions to avoiding infinity loop in the parser, depending on which you're using (RL or LL).

Anyway, the real answer for your question is: you can't do what you want only with regex, i'll need more concepts.

Upvotes: 3

Related Questions