Code Monkey home page Code Monkey logo

Comments (3)

foonathan avatar foonathan commented on May 22, 2024

You're just needing a lower upper limit, right? If so, I'd be happy with lexy::bounded<T, Max>.

Your proposed bounded<T, Min, Max> has a couple of issues. The first is a bug: add_digit_checked() is only called for the last digit: if the number has fewer than max digits, it won't be called and as such, the Min check never happens. The fix would require an additional validate function that is called by the parser after the entire number has been constructed. The second is philosophical: the only reason dsl::integer does overflow checking is that the digits won't fit into the integer otherwise - there is nothing it can do. In principle, it would be cleaner if lexy doesn't need to produce any overflow errors, those aren't really syntax errors, but more semantic errors that should be handled at a later stage.

As such,I feel that checking whether e.g. a register is out of bounds isn't something that lexy should do, it's something that should be handled later, just like any other semantic errors. I'm already making an exception for overflow in general and lexy::code_point in particular, so checking for a maximal value is probably okay, but I don't like the min check. Does it make sense to you what I'm saying? :D

from lexy.

studoot avatar studoot commented on May 22, 2024

Your proposed bounded<T, Min, Max> has a couple of issues. The first is a bug: add_digit_checked() is only called for the last digit: if the number has fewer than max digits, it won't be called and as such, the Min check never happens.

You're quite right - I hadn't fully thought that part of it through, and as I hadn't done any test cases for the minimum bound checking, I hadn't discovered it that way yet!

As such,I feel that checking whether e.g. a register is out of bounds isn't something that lexy should do, it's something that should be handled later, just like any other semantic errors. I'm already making an exception for overflow in general and lexy::code_point in particular, so checking for a maximal value is probably okay, but I don't like the min check. Does it make sense to you what I'm saying? :D

Yeah, that makes sense - I think my preference for doing some semantic checking within the parser (and potentially getting better locality of where the error occurred within the input text) probably comes from ANTLR's semantic predicates, as ANTLR was the first parser generator I used. And, to be honest, most of the range checking I want to do in a parser tends to be of unsigned integers, so the Min bound is less useful.

I'll put together a PR with a bounded<T, Max> and take it from there.

from lexy.

foonathan avatar foonathan commented on May 22, 2024

I'll put together a PR with a bounded<T, Max> and take it from there.

Thanks, please also try and use it to implement the specialization for lexy::code_point.

from lexy.

Related Issues (20)

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.