Microsoft Developer Network > Forenhomepage > Microsoft SQL Server Modeling > MGrammar: using tabs to denote blocks
Stellen Sie eine FrageStellen Sie eine Frage
 

BeantwortetMGrammar: using tabs to denote blocks

  • Dienstag, 2. Dezember 2008 19:47justncase80 TeilnehmermedaillenTeilnehmermedaillenTeilnehmermedaillenTeilnehmermedaillenTeilnehmermedaillen
     Enthält Code
    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??

Antworten

  • Donnerstag, 4. Dezember 2008 07:35vbbalaMSFTTeilnehmermedaillenTeilnehmermedaillenTeilnehmermedaillenTeilnehmermedaillenTeilnehmermedaillen
     Beantwortet
     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.
    • Als Antwort vorgeschlagenMike Weinhardt Freitag, 5. Dezember 2008 19:37
    • Als Antwort markiertjustncase80 Sonntag, 7. Dezember 2008 04:32
    •  

Alle Antworten

  • Dienstag, 2. Dezember 2008 23:55justncase80 TeilnehmermedaillenTeilnehmermedaillenTeilnehmermedaillenTeilnehmermedaillenTeilnehmermedaillen
     
    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?
  • Mittwoch, 3. Dezember 2008 14:52justncase80 TeilnehmermedaillenTeilnehmermedaillenTeilnehmermedaillenTeilnehmermedaillenTeilnehmermedaillen
     Enthält Code
    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.
  • Donnerstag, 4. Dezember 2008 07:35vbbalaMSFTTeilnehmermedaillenTeilnehmermedaillenTeilnehmermedaillenTeilnehmermedaillenTeilnehmermedaillen
     Beantwortet
     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.
    • Als Antwort vorgeschlagenMike Weinhardt Freitag, 5. Dezember 2008 19:37
    • Als Antwort markiertjustncase80 Sonntag, 7. Dezember 2008 04:32
    •  
  • Sonntag, 7. Dezember 2008 04:32justncase80 TeilnehmermedaillenTeilnehmermedaillenTeilnehmermedaillenTeilnehmermedaillenTeilnehmermedaillen
     
    what is a token event listener?
  • Dienstag, 23. Juni 2009 10:11Martin Dvorak -1- TeilnehmermedaillenTeilnehmermedaillenTeilnehmermedaillenTeilnehmermedaillenTeilnehmermedaillen
     
    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-
  • Dienstag, 23. Juni 2009 14:55justncase80 TeilnehmermedaillenTeilnehmermedaillenTeilnehmermedaillenTeilnehmermedaillenTeilnehmermedaillen
     
    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. 
  • Mittwoch, 24. Juni 2009 10:45Martin Dvorak -1- TeilnehmermedaillenTeilnehmermedaillenTeilnehmermedaillenTeilnehmermedaillenTeilnehmermedaillen
     
    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-