MGrammar: using tabs to denote blocks
- Suppose I wanted something like this
if e1: while e2: call() else return e3 call()
Meaning, if you have the same number of whitespace characters as the line above you then you're in the same block. The ":" denotes the end of a block a decrease in indentation denotes an end of a block.
How the heck do I do this??
解答
- In the current bits, there is no easy way to do this -- perhaps registering a token event listener for the indenting whitespace may get you closer to doing this manually. For the next drop, we are already looking into generating 'Indent' and 'Unindent' tokens from the lexer. That will make it easier to support scoping by indentation.
- 已提議為解答Mike Weinhardt 2008年12月5日 下午 07:37
- 已標示為解答justncase80 2008年12月7日 上午 04:32
所有回覆
- So far I'm thinking I can just keep track of the whitespace since the last newline and in the code behind just do the logic check. Does anyone second that idea?
- After messing with this for a while I was able to sort of come up with something:
module count { import Language; import Microsoft.Languages; export test; language test { syntax Main = l:Line* => Lines[valuesof(l)]; syntax Line = "-" NL | l:L(" ", "-") => Indent[l]; syntax L(previous, valid) = previous v:valid NL => v | previous l:L(" ", valid) => Indent[l]; token NL = Base.NewLine; } }
This doesn't quite enforce indentations (couldn't figure out how to do that) but it does tell you how indented you are in each line. Somehow what I need to do is to use this to determine the start of code blocks... I think I'm on the right trail. I tried to do something like this:
token Indent(i) = " " " "#i..; // at least 1 more space
But that doesn't work, you can't have a parameter be apart of the number constraints. Those appear to be hard coded. - In the current bits, there is no easy way to do this -- perhaps registering a token event listener for the indenting whitespace may get you closer to doing this manually. For the next drop, we are already looking into generating 'Indent' and 'Unindent' tokens from the lexer. That will make it easier to support scoping by indentation.
- 已提議為解答Mike Weinhardt 2008年12月5日 下午 07:37
- 已標示為解答justncase80 2008年12月7日 上午 04:32
- what is a token event listener?
- Hi vbbala,
Are there some news on this front, i.e. are there some new features in current bits which would make it easier to support scoping by indentation? Or are there any planned for next CTP release? I've been looking for 'Indent' and 'Unindent' you mention in current M language specification (May 09 CTP) but found nothing.
Thanks, Martin
-md- - I don't think it's improved much on this front. There is a new "nest" keyword which I haven't looked into which might make it more feasible. But I think the answer is that you have to not interleave whitespace but instead carefully create tokens for keeping track of indentation.
- Thanks for reply. Since my language is really simple with maximum three levels of indentation, I solved this by simply interleaving all whitespace except new line characters and handling new line characters in syntax/tokens. Nevertheless I still hope M will somehow support scoping by indentation in the future because it is very useful for simple easily readable custom languages.
-md-

