Comments (3)
After #20 I started to play around with making mecha take memory allocations. I agree with parse failures being errors and not attempting to get mecha to manage memory. I have a couple of questions:
- Should mecha attempt to enforce the use of arena allocator? In my experiments with adding memory allocation it became apparent that it would be basically impossible to clean up the memory by hand as a parse failure halfway through the chain would give you nothing to work with.
- What is the point of
OtherError
in the proposed error set? - Should all parsers, regardless of if they allocate or not, take an allocator?
In my experiments I thought it would nice/interesting if as a user knew mecha was going to attempt to allocate memory. To that end I defined two types of parsers:
/// The type of all parsers that can work with `mecha`
pub fn Parser(comptime T: type) type {
return fn (void, []const u8) ParseError!Result(T);
}
/// A parser which works with `mecha` and is allowed to allocate
pub fn ParserAlloc(comptime T: type) type {
return fn (*mem.Allocator, []const u8) ParseAllocError!Result(T);
}
With the two associated error sets
pub const ParseError = error{ParseFailed};
pub const ParseAllocError = ParseError || mem.Allocator.Error;
I was able to get it working with not too much headache over on branch which no longer exists Not sure if others think that its worth the extra complexity to allow for such behavior however.
from mecha.
* Should mecha attempt to enforce the use of arena allocator? In my experiments with adding memory allocation it became apparent that it would be basically impossible to clean up the memory by hand as a parse failure halfway through the chain would give you nothing to work with.
There are many allocation schemes that are able to clean up the entire "pool", such as ArenaAllocator
and FixedBufferAllocator
. One could even make an allocator that just keeps track of a list of allocations, so that they can all be free at once if desired. I think it is best to just take an *mem.Allocator
and let the user pick the best allocator for their needs.
* What is the point of `OtherError` in the proposed error set?
For conversions where the user of mecha wants to return an error which is not OutOfMemory
. They could then look somethink up in their Context
to figure out more details about their own specific error.
* Should all parsers, regardless of if they allocate or not, take an allocator?
I think the library should always take an allocator to keep the whole library simple. If the user wonna make sure the parser never allocates, they can pass a FailingAllocator
and catch |err| switch (err) { error.OutOfMemory => unreachable }
. I don't think this is unreasonable.
from mecha.
Let's leave this open until we have a version of many
that collects the results. I have an API in mind that only has one many
function that takes a set of options.
from mecha.
Related Issues (20)
- Delimiter seperated value parser
- Have an alternative to mecha.convert for when the convertion cannot fail HOT 1
- Example Rgb parser is incorrect
- Documentation HOT 1
- How to parse bareword literal? HOT 2
- Build documentation from doc comments HOT 1
- How to parse indents? HOT 4
- How to parse string literals? HOT 2
- Returning progress when parsing fails HOT 8
- most mecha.ascii.* parsers are Parser(u8), but mecha.ascii.char is Parser(void) HOT 1
- Create zig-master branch HOT 1
- Infinite recursion - i.e. how to use ref()? HOT 2
- Document what `map` passes to functions with various parsers (`combine`, `oneOf`, ...) HOT 2
- Line/column number tracking and custom errors HOT 1
- Support the new zig package manager HOT 2
- Build including mecha fails with no field named 'root_source_file' in struct 'Build.CreateModuleOptions' HOT 2
- tag 0.11.0 HOT 1
- Combine yields incompatible tuple types HOT 3
- Update to the latest Zig HOT 3
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from mecha.