MSDN > Home page del forum > Microsoft SQL Server Modeling > MGrammar: using tabs to denote blocks
Formula una domandaFormula una domanda
 

Con rispostaMGrammar: using tabs to denote blocks

  • martedì 2 dicembre 2008 19.47justncase80 Medaglie utenteMedaglie utenteMedaglie utenteMedaglie utenteMedaglie utente
     Contiene codice
    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??

Risposte

  • giovedì 4 dicembre 2008 7.35vbbalaMSFTMedaglie utenteMedaglie utenteMedaglie utenteMedaglie utenteMedaglie utente
     Con risposta
     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.
    • Proposto come rispostaMike Weinhardt venerdì 5 dicembre 2008 19.37
    • Contrassegnato come rispostajustncase80 domenica 7 dicembre 2008 4.32
    •  

Tutte le risposte

  • martedì 2 dicembre 2008 23.55justncase80 Medaglie utenteMedaglie utenteMedaglie utenteMedaglie utenteMedaglie utente
     
    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?
  • mercoledì 3 dicembre 2008 14.52justncase80 Medaglie utenteMedaglie utenteMedaglie utenteMedaglie utenteMedaglie utente
     Contiene codice
    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.
  • giovedì 4 dicembre 2008 7.35vbbalaMSFTMedaglie utenteMedaglie utenteMedaglie utenteMedaglie utenteMedaglie utente
     Con risposta
     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.
    • Proposto come rispostaMike Weinhardt venerdì 5 dicembre 2008 19.37
    • Contrassegnato come rispostajustncase80 domenica 7 dicembre 2008 4.32
    •  
  • domenica 7 dicembre 2008 4.32justncase80 Medaglie utenteMedaglie utenteMedaglie utenteMedaglie utenteMedaglie utente
     
    what is a token event listener?
  • martedì 23 giugno 2009 10.11Martin Dvorak -1- Medaglie utenteMedaglie utenteMedaglie utenteMedaglie utenteMedaglie utente
     
    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-
  • martedì 23 giugno 2009 14.55justncase80 Medaglie utenteMedaglie utenteMedaglie utenteMedaglie utenteMedaglie utente
     
    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. 
  • mercoledì 24 giugno 2009 10.45Martin Dvorak -1- Medaglie utenteMedaglie utenteMedaglie utenteMedaglie utenteMedaglie utente
     
    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-