Skip to content

Commit

Permalink
added equivalents to operators & and !, so that support for PEG gramm…
Browse files Browse the repository at this point in the history
…ars is now obvious
  • Loading branch information
Fabien committed Jan 13, 2011
1 parent ca44b47 commit 4020f6e
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 2 deletions.
2 changes: 1 addition & 1 deletion src/CoffeeScriptParser.coffee
Expand Up @@ -511,7 +511,7 @@ cs.parse = (parser, src) ->
parser = cs[parser]
else unless parser instanceof gg.Parser
throw new Error "bad args"
lexer = new lex.Lexer(src, cs.keywords)
lexer = new lex.Lexer(src, cs.keywords)
stream = new lex.Stream lexer
# print("\nTokens: \n#{stream.tokens.join('\n')}\n\n")
parser.parse stream
Expand Down
31 changes: 30 additions & 1 deletion src/GrammarGenerator.coffee
Expand Up @@ -735,4 +735,33 @@ exports.zero = lift -> fail

exports.named = (name, parser) ->
parser.name = name
return parser
return parser

# Equivalent to PEG's '&': parse like @primary, but doesn't consume any token
exports.couldParse = (x...) -> new CouldParse(x...)
exports.CouldParse = class CouldParse extends Parser
constructor: (@primary) ->
super
@epsilon = "always"
primary.addListener @
@notify()

reindexInternal: ->
@primary.reindex()
@keys = @primary.keys

parseInternal: (lx, args...) ->
bookmark = lx.save()
result = @primary.parse(lx, args...)
lx.restore bookmark
return result

# Equivalent to PEG's '!': fail if @primary could parse here,
# return true otherwise.
exports.couldntParse = (x...) -> new CouldntParse(x...)
exports.CouldntParse = class CouldntParse extends CouldParse
reindexInternal: -> @keys = false
parseInternal: (lx) ->
result = super
if result == fail then return true else return fail
toString: -> "!(" + @primary.toString() + ")"

0 comments on commit 4020f6e

Please sign in to comment.