Skip to content

Commit d18df42

Browse files
Refactoring and CI
. . .
1 parent 5a8a942 commit d18df42

File tree

5 files changed

+250
-233
lines changed

5 files changed

+250
-233
lines changed

.github/workflows/ci.yml

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
name: parcom ci
2+
3+
on:
4+
push:
5+
branches:
6+
- main
7+
pull_request:
8+
paths-ignore:
9+
- 'LICENSE'
10+
- 'README.md'
11+
12+
jobs:
13+
test:
14+
runs-on: ubuntu-latest
15+
name: Build and Test
16+
steps:
17+
- uses: actions/checkout@v3
18+
- uses: mlugg/setup-zig@v1
19+
- run: zig build test --summary all

README.md

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,13 @@
11
# parcom
22

3+
[![parcom ci](https://github.com/dokwork/parcom/actions/workflows/ci.yml/badge.svg)](https://github.com/dokwork/parcom/actions/workflows/ci.yml)
4+
35
> [!WARNING]
46
> Required version of zig is 0.13.0
57
68
Parser combinators for Zig.
79

8-
Read documentation here: [https://dokwork.github.io/parcom/index.html](https://dokwork.github.io/parcom/index.html)
10+
Documentation: [https://dokwork.github.io/parcom/index.html](https://dokwork.github.io/parcom/index.html)
11+
12+
Examples:
13+
- [The parser of a math expression](examples/expression.zig)

build.zig

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -31,24 +31,24 @@ pub fn build(b: *std.Build) void {
3131
// to handle many examples see
3232
// https://github.com/zigzap/zap/blob/master/build.zig#L49
3333

34-
const example = b.addExecutable(.{
35-
.name = "expression",
36-
.root_source_file = b.path("example/expression.zig"),
34+
const examples = b.addExecutable(.{
35+
.name = "examples",
36+
.root_source_file = b.path("examples/expression.zig"),
3737
.target = target,
3838
.optimize = optimize,
3939
});
4040

41-
example.root_module.addImport("parcom", parcom);
41+
examples.root_module.addImport("parcom", parcom);
4242

43-
b.installArtifact(example);
43+
b.installArtifact(examples);
4444

45-
const run_example = b.addRunArtifact(example);
45+
const run_examples = b.addRunArtifact(examples);
4646
if (b.args) |args| {
47-
run_example.addArgs(args);
47+
run_examples.addArgs(args);
4848
}
4949

50-
const run_step = b.step("example", "Run example of parsing an expression");
51-
run_step.dependOn(&run_example.step);
50+
const run_step = b.step("expression", "Run example of parsing a math expression");
51+
run_step.dependOn(&run_examples.step);
5252

5353
// ----- Unit tests -----
5454
const test_filters = b.option([]const []const u8, "test-filter", "Skip tests that do not match any filter") orelse &[0][]const u8{};
@@ -63,17 +63,17 @@ pub fn build(b: *std.Build) void {
6363
const run_parcom_tests = b.addRunArtifact(parcom_tests);
6464

6565
const example_tests = b.addTest(.{
66-
.root_source_file = b.path("example/expression.zig"),
66+
.root_source_file = b.path("examples/expression.zig"),
6767
.target = target,
6868
.optimize = optimize,
6969
.filters = test_filters,
7070
});
7171

7272
example_tests.root_module.addImport("parcom", parcom);
7373

74-
const run_example_tests = b.addRunArtifact(example_tests);
74+
const run_examples_tests = b.addRunArtifact(example_tests);
7575

7676
const test_step = b.step("test", "Run unit tests");
7777
test_step.dependOn(&run_parcom_tests.step);
78-
test_step.dependOn(&run_example_tests.step);
78+
test_step.dependOn(&run_examples_tests.step);
7979
}
Lines changed: 12 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,13 @@
1-
//! This is a small example of the mathematics expression parser,
1+
//! This is a small example of a math expression parser,
22
//! which parse only unsigned integers, brackets, [+-*/] operations,
33
//! and no spaces.
44
//!
55
//! This example can be run with expression at first argument:
66
//! ```
7-
//! zig build example -- "1+1"
7+
//! zig build expression -- "1+1"
88
//! ```
99
const std = @import("std");
10-
const parcom = @import("parcom");
11-
12-
const P = parcom.Parsers;
10+
const p = @import("parcom");
1311

1412
const Value = i8;
1513

@@ -47,7 +45,7 @@ const Operation = enum {
4745
/// Product ← Value (('*' / '/') Value)*
4846
/// Value ← [0-9]+ / '(' Expr ')'
4947
/// ```
50-
fn expr(alloc: std.mem.Allocator) !parcom.TaggedParser(Value) {
48+
fn expr(alloc: std.mem.Allocator) !p.TaggedParser(Value) {
5149
// functions to transform parsers results
5250
const fns = struct {
5351
fn parseInt(_: void, str: [10:0]u8) anyerror!Value {
@@ -57,7 +55,7 @@ fn expr(alloc: std.mem.Allocator) !parcom.TaggedParser(Value) {
5755
fn valueFromParens(_: void, result: struct { u8, Value, u8 }) anyerror!Value {
5856
return result[1];
5957
}
60-
fn valueFromEither(_: void, result: parcom.Either(Value, Value)) anyerror!Value {
58+
fn valueFromEither(_: void, result: p.Either(Value, Value)) anyerror!Value {
6159
return switch (result) {
6260
.left => result.left,
6361
.right => result.right,
@@ -79,12 +77,12 @@ fn expr(alloc: std.mem.Allocator) !parcom.TaggedParser(Value) {
7977
};
8078

8179
// brackets: Int <- (<exp>)
82-
const brackets = P.tuple(.{ P.char('('), P.lazy(Value, alloc, expr), P.char(')') })
80+
const brackets = p.tuple(.{ p.char('('), p.lazy(Value, alloc, expr), p.char(')') })
8381
.transform(Value, {}, fns.valueFromParens);
8482

8583
// we can't use Parsers.int to parse numbers here to avoid consumption of the '-' and '+'
8684
// number: Int <- \d+
87-
const number = P.range('0', '9').repeatToSentinelArray(1, 10)
85+
const number = p.range('0', '9').repeatToSentinelArray(1, 10)
8886
.transform(Value, {}, fns.parseInt);
8987

9088
// value: Int <- <number>|<brackets>
@@ -93,14 +91,14 @@ fn expr(alloc: std.mem.Allocator) !parcom.TaggedParser(Value) {
9391

9492
// product: Int <- <value>([*/]<value>)*
9593
const product = blk: {
96-
const operation = P.oneCharOf("*/").transform(Operation, {}, Operation.parse);
94+
const operation = p.oneCharOf("*/").transform(Operation, {}, Operation.parse);
9795
break :blk value.andThen(operation.andThen(value).repeat(alloc))
9896
.transform(Value, alloc, fns.calculate);
9997
};
10098

10199
// sum: Int <- <product>([+-]<product>)*
102100
const sum = blk: {
103-
const operation = P.oneCharOf("+-").transform(Operation, {}, Operation.parse);
101+
const operation = p.oneCharOf("+-").transform(Operation, {}, Operation.parse);
104102
break :blk product.andThen(operation.andThen(product).repeat(alloc))
105103
.transform(Value, alloc, fns.calculate);
106104
};
@@ -109,9 +107,9 @@ fn expr(alloc: std.mem.Allocator) !parcom.TaggedParser(Value) {
109107
}
110108

111109
fn evaluate(alloc: std.mem.Allocator, expression: []const u8) !?Value {
112-
const p = try expr(alloc);
113-
defer p.deinit();
114-
return try p.parseString(alloc, expression);
110+
const parser = try expr(alloc);
111+
defer parser.deinit();
112+
return try parser.parseString(alloc, expression);
115113
}
116114

117115
test "1+1" {

0 commit comments

Comments
 (0)