batch file grammar

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Michael Kujawa
    Enthusiast
    • Oct 2007
    • 46

    batch file grammar

    I'm trying to put together a grammar for cmd batch files, and I'm stumbling on the precedence issue with my keywords. If I just list out the keywords, then those keywords will be detected in innappropriate places (http://www.scootersoftware.com/vbull...ead.php?t=3773). So I tried to create a catch-all rule (type String) to be my lowest priority rule, but I can't use the typical identifier rule because there are so many legal characters. Consider the following:
    Code:
    :foo
      set F=%~n2
      call :sub /Oo..\call\1.0-3_4*
    The first line should be a label. On the second line, I want the set, the =, and the %~n2 to be a keyword, operator, and identifier respectively. On the third line, I want the call, the :sub, and the rest to be a keyword, label, and string respectively.

    my catch-all is currently the Basic, regex
    Code:
    [^ %=()]+
    This works for lines 2 and 3, but it's determining :foo to be a String instead of a label. Label is defined as a Basic, Regex
    Code:
    :(\w|_)+
    If I put a space after the :foo, it recognizes it as a label. Does BC think String matches more characters because it matches the ^ and $ as well? I'm at a loss.

    I am really struggling with this grammar, having spent a couple of hours on it now. It seems like the "List" primitive isn't powerful enough, and the "most characters" thing (http://www.scootersoftware.com/vbull...ead.php?t=3376) was not at all obvious (and not in the help for the grammar tab.)
  • Michael Bulgrien
    Carpal Tunnel
    • Oct 2007
    • 1772

    #2
    There is already a basic batch grammar file available for download here.

    It may not have the complexity you are looking for, but perhaps you could use it as a starting point?
    BC v4.0.7 build 19761
    ¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯

    Comment

    • Michael Bulgrien
      Carpal Tunnel
      • Oct 2007
      • 1772

      #3
      Originally posted by Michael Kujawa
      Label is defined as a Basic, Regex
      Code:
      :(\w|_)+
      Try adding begin-of-line ^ and end-of-line $ characters:

      Code:
      ^\w*:(\w|_$)+
      BC v4.0.7 build 19761
      ¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯

      Comment

      • Michael Kujawa
        Enthusiast
        • Oct 2007
        • 46

        #4
        I didn't include the line ending characters because I wanted the label to be highlighted in the goto and call statements as well.

        I downloaded the bcpkg you suggested and tweaked it up. It uses [_a-z]\w* as its catch-all identifier, which seems to work well enough (even though it doesn't even match the entire identifier.)

        I think I discovered at least one of my major troubles: when editing the grammar and choosing 'OK', the reparse isn't always correct. In other words, if I then close the diff and re-open it, I will sometimes get different results! I suspect I had tried many correct things but been deceived by this.

        My biggest changes were to expand the definition of a variable (which I changed to Environment Variable) to include all of the wacky path and substring parameters.

        Now I have useful syntax highlighting for my complicated batch files, and I am happy Thanks for your help

        Comment

        • Aaron
          Team Scooter
          • Oct 2007
          • 16000

          #5
          Hello Michael,

          I would expect BC to refresh after changing your file formats. Let's make sure we are seeing the same thing. Any specific information about your edits would be useful, including:

          Could you send in a pair of screenshots demonstrating this? Or

          Could you send a pair or example files and 2 BC Support Packages (Help menu -> Support; Export). One with your rules pre-changes, and one post-changes.

          Please email the above to [email protected] with a link to this post.

          Thanks.
          Aaron P Scooter Software

          Comment

          • Michael Bulgrien
            Carpal Tunnel
            • Oct 2007
            • 1772

            #6
            Also, I would be interested in seeing Michael K's tweaked batch file format as I, too, have a considerable number of complex batch files. If, comparing against my own files, I don't find any issues with the tweaked rules, it might be nice to adopt them as a replacement for the package currently available on the Scooter site.
            BC v4.0.7 build 19761
            ¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯

            Comment

            • Michael Kujawa
              Enthusiast
              • Oct 2007
              • 46

              #7
              I'm not able to reproduce what I saw earlier (I changed the order of the grammar rules, which inexplicably fixed something. When I changed the order back, it still worked.)

              I have found another problem: If you change the entry for Label in the downloadable batch grammar from "Basic" to "List" but otherwise keep it the same, the regex no longer works. (I did check "Regular expressions. Other expressions added to the list work.) Since my initial problem was with matching labels, this may have been what I was actually bumping in to.

              I did a pass on my grammar looking for special cases, so hopefully it's worthy for other people to use.

              Comment

              • Michael Bulgrien
                Carpal Tunnel
                • Oct 2007
                • 1772

                #8
                Thanks, Michael

                I checked out the batch file format on my command scripts, and everything looks fine.
                BC v4.0.7 build 19761
                ¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯

                Comment

                Working...