MSDN > フォーラム ホーム > Microsoft SQL Server Modeling > MGrammar: using tabs to denote blocks
質問する質問する
 

回答済みMGrammar: using tabs to denote blocks

  • 2008年12月2日 19:47justncase80 ユーザーのメダルユーザーのメダルユーザーのメダルユーザーのメダルユーザーのメダル
     コードあり
    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??

回答

  • 2008年12月4日 7:35vbbalaMSFTユーザーのメダルユーザーのメダルユーザーのメダルユーザーのメダルユーザーのメダル
     回答済み
     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.

すべての返信

  • 2008年12月2日 23:55justncase80 ユーザーのメダルユーザーのメダルユーザーのメダルユーザーのメダルユーザーのメダル
     
    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?
  • 2008年12月3日 14:52justncase80 ユーザーのメダルユーザーのメダルユーザーのメダルユーザーのメダルユーザーのメダル
     コードあり
    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.
  • 2008年12月4日 7:35vbbalaMSFTユーザーのメダルユーザーのメダルユーザーのメダルユーザーのメダルユーザーのメダル
     回答済み
     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.
  • 2008年12月7日 4:32justncase80 ユーザーのメダルユーザーのメダルユーザーのメダルユーザーのメダルユーザーのメダル
     
    what is a token event listener?
  • 2009年6月23日 10:11Martin Dvorak -1- ユーザーのメダルユーザーのメダルユーザーのメダルユーザーのメダルユーザーのメダル
     
    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-
  • 2009年6月23日 14:55justncase80 ユーザーのメダルユーザーのメダルユーザーのメダルユーザーのメダルユーザーのメダル
     
    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. 
  • 2009年6月24日 10:45Martin Dvorak -1- ユーザーのメダルユーザーのメダルユーザーのメダルユーザーのメダルユーザーのメダル
     
    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-