Skip to content Skip to sidebar Skip to footer

Make A Grammar Expression For STRING.STRING.STRING On PEG.js

I am looking for a peg.js grammar expression for matching against: 'variable' # Fails 'variable.' # Fails '' # Fails 'variable.variable' # Ok 'variable.variable.variable.variable.

Solution 1:

Here's what I came up with to get rid of the "." characters. I'll admit that I've never used peg.js before :)

PATH_EXP =    
    (first:STRING_EXP rest:("." STRING_EXP)*) {
      return {
        PATH: first +  
              rest.map(function(v) {
                return v[1]; 
              }).join("")
      };
    }

edit — oh wait this is better:

PATH_EXP = 
    first:STRING_EXP rest:("." s:STRING_EXP { return "." + s; })+ {
      return {
        PATH: first + rest.join('')
      };
    }

edit — clearly if you want the "." characters you'd include them in the action inside that second part. Missed that part of the question.


Post a Comment for "Make A Grammar Expression For STRING.STRING.STRING On PEG.js"