MarpaX-ESLIF

 view release on metacpan or  search on metacpan

lib/MarpaX/ESLIF/BNF.pod  view on Meta::CPAN

        self._input = input
        self._nbParameterizedRhsCalls = 0
     end,
     ["read"]                   = function(self) self._data = self._input return true end,
     ["isEof"]                  = function(self) return true end,
     ["isCharacterStream"]      = function(self) return true end,
     ["encoding"]               = function(self) return nil end,
     ["data"]                   = function(self) return self._data end,
     ["isWithDisableThreshold"] = function(self) return false end,
     ["isWithExhaustion"]       = function(self) return false end,
     ["isWithNewline"]          = function(self) return true end,
     ["isWithTrack"]            = function(self) return false end,
     ["parameterizedRhs"]       = function(self, parameter)
        self._nbParameterizedRhsCalls = self._nbParameterizedRhsCalls + 1

        local output
        if (self._nbParameterizedRhsCalls == 5) then
          output = "'5'"
        elseif (self._nbParameterizedRhsCalls > 5) then
          output = "'no match'"
        else
          parameter = parameter + 1
          output = ". => parameterizedRhs->("..parameter..")"
        end
        return output
     end
  }

  local valueInterface = {
     ["isWithHighRankOnly"] = function(self) return true end,
     ["isWithOrderByRank"]  = function(self) return true end,
     ["isWithAmbiguous"]    = function(self) return false end,
     ["isWithNull"]         = function(self) return false end,
     ["maxParses"]          = function(self) return 0 end,
     ["getResult"]          = function(self) return self._result end,
     ["setResult"]          = function(self, result) self._result = result end,
  }

  local logger = {
     ["trace"]     = function(self, msgs) self:tracef("%s", msgs) end,
     ["debug"]     = function(self, msgs) self:debugf("%s", msgs) end,
     ["info"]      = function(self, msgs) self:infof("%s", msgs) end,
     ["notice"]    = function(self, msgs) self:noticef("%s", msgs) end,
     ["warning"]   = function(self, msgs) self:warningf("%s", msgs) end,
     ["error"]     = function(self, msgs) self:errorf("%s", msgs) end,
     ["critical"]  = function(self, msgs) self:criticalf("%s", msgs) end,
     ["emergency"] = function(self, msgs) self:emergencyf("%s", msgs) end,
     --
     -- Used by us
     --
     ["tracef"]     = function(self, fmts, ...) print(string.format("%-9s "..fmts, 'TRACE', ...)) end,
     ["debugf"]     = function(self, fmts, ...) print(string.format("%-9s "..fmts, 'DEBUG', ...)) end,
     ["infof"]      = function(self, fmts, ...) print(string.format("%-9s "..fmts, 'INFO', ...)) end,
     ["noticef"]    = function(self, fmts, ...) print(string.format("%-9s "..fmts, 'NOTICE', ...)) end,
     ["warningf"]   = function(self, fmts, ...) print(string.format("%-9s "..fmts, 'WARN', ...)) end,
     ["errorf"]     = function(self, fmts, ...) print(string.format("%-9s "..fmts, 'ERROR', ...)) end,
     ["criticalf"]  = function(self, fmts, ...) print(string.format("%-9s "..fmts, 'CRITICAL', ...)) end,
     ["emergencyf"] = function(self, fmts, ...) print(string.format("%-9s "..fmts, 'EMERGENCY', ...)) end
  }

  local marpaESLIFLua = require 'marpaESLIFLua'
  local marpaESLIFp = marpaESLIFLua.MarpaX::ESLIF::new(logger)

  marpaESLIFGrammarp = marpaESLIFp:marpaESLIFGrammar_new([[
  :default ::= action => ::shift
  top ::= . => parameterizedRhs->(1)
        | . => parameterizedRhs->(2)
        | . => parameterizedRhs->(3)
        | . => parameterizedRhs->(4)
        | . => ::luac->function(x)
                         return "'will not match'"
                       end
               ->(5)
        | . => ::luac->function(x)
                         print('Called with x='..x)
                         return "'will not match'"
                       end
               ->(15)
        | . => ::lua->grammar_ok->(10,12)
        | . => ::lua->grammar_ko->(10,12)
        | . => ::lua->action_raising_error->(10,12)
        | . => ::lua->unknown_action->(10,12)

  <luascript>
  function grammar_ok(x,y)
    return "'Y'"
  end
  function grammar_ko(x,y)
    return "Y"
  end
  function action_raising_error(x,y)
    error('Errors are trapped...')
  end
  </luascript>
  ]])

  recognizerInterface:init('5')
  marpaESLIFGrammarp:parse(recognizerInterface, valueInterface)
  logger:noticef('... Grammar parse result: %s', valueInterface:getResult())

Output is:

  Called with x=15
  ERROR     Looking at rules in grammar level 0 (Grammar level 0): symbol 1 (Y) must be resolved as <Y> in grammar at level 0 or 1
  ERROR     grammar_ko callback returned a string that cannot be converted to a grammar
  ERROR     <luascript/>:9: Errors are trapped...
  ERROR     action_raising_error callback failed
  ERROR     <luascript/>:9: No such function unknown_action
  ERROR     unknown_action callback failed
  NOTICE    ... Grammar parse result: 5

=item Lookahead

Any RHS surrounded by C<(?=...)> or C<(?!...)> is interpreted to a positive or negative lookahead, respectively. Internally, it is processed like a lexeme, and will result in a zero-length lexeme in case of match. You can skip it in the rule valuatio...

  lhs ::= (-(?= /anything/ )-)

will do a positive lookahead of regular expression C</anything/> and skip it in the rule's valuation.

=back



( run in 2.376 seconds using v1.01-cache-2.11-cpan-99c4e6809bf )