Git Product home page Git Product logo

Comments (5)

Harleqin avatar Harleqin commented on September 24, 2024 1

Yes, but also extending grammars, or importing rules from other grammars might be possible. It is quite similar to packages and symbols, but I'd tend not to try to map the former to the latter, because they operate on different semantic levels.

from cl-bnf.

diasbruno avatar diasbruno commented on September 24, 2024

@Harleqin Thanks for bring this up. I'm still thinking on a better (proper) way to write the rules. I'll do some work on the weekend to improve this. Any suggestions?

from cl-bnf.

Harleqin avatar Harleqin commented on September 24, 2024

The obvious solution that changes as little as possible is to actually name the macro cl-bnf:|:=|. You'd need to write (defmacro |:=| …) or (defmacro \:= …), and every invocation would need to be changed as well.

Going a bit further would be to rename it to defrule or define-rule. This would also make it clearer that it is a toplevel definition (I had to look twice at the documentation to understand this).

Thinking a bit more about it, as a user I'd maybe not want the rules to be global, but always part of a grammar, so that I can define several grammars in one program without name clashes. You can then maybe keep the :=, because it would be interpreted by a defgrammar macro, instead of being a macro itself.

(defgrammar json-number ()
  (:= decimal-number (:many (:one #'numeric-char-p)))
  (:= real-number (:or (:and #'decimal-number
                             (:char #\.)
                             #'decimal-number)
                       (:and #'decimal-number
                             (:char #\.))))
  (:= signed-part (:or (:char #\+) (:char #\-)))
  (:= exp-chars (:or (:char #\e)
                     (:char #\E)))
  (:= exp-part (:or (:and #'exp-chars
                          #'signed-part
                          #'decimal-number)
                    (:and #'exp-chars
                          #'decimal-number)))
  (:= numeric (:or #'real-number
                   #'decimal-number))
  (:= number-literal (:or (:and #'numeric
                                #'exp-part)
                          #'numeric)
      :call (lambda (matches)
              (cons :number (stringify matches))))

(parse json-number number-literal "1e3")

from cl-bnf.

diasbruno avatar diasbruno commented on September 24, 2024

Yeah, that's pretty much what I had in mind. See #5.

Perhaps, we can keep both...the defgrammar and defrule.

(defrule real-number 'the rule' :apply ...))
(defrule decimal-number 'the rule' :apply ...))

(defgrammar json-parser ()
  (number := (:or #'real-number #'decimal-number) :apply ...)
  (strings := 'rule' :apply ...)
  (...)
  (parse number strings objects array))

Maybe this way, we can allow sharing rules.

from cl-bnf.

diasbruno avatar diasbruno commented on September 24, 2024

Both macros are available now. Thanks for all the help and ideas.

from cl-bnf.

Related Issues (13)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.