90 Commits

Author SHA1 Message Date
Tom Alexander
678106bb65 Use tekton matrix for specifying all combinations of features for the build CI job.
All checks were successful
rust-test Build rust-test has succeeded
2023-08-25 03:56:08 -04:00
Tom Alexander
19432d91ab Get the emacs and org-mode versions when launching the compare script.
All checks were successful
rustfmt Build rustfmt has succeeded
rust-test Build rust-test has succeeded
rust-build Build rust-build has succeeded
2023-08-25 03:11:19 -04:00
Tom Alexander
16a107eebb Update org-mode version. 2023-08-25 02:56:28 -04:00
Tom Alexander
77348b560c Parameterize the emacs and org-mode versions in the dockerfiles.
All checks were successful
rustfmt Build rustfmt has succeeded
rust-build Build rust-build has succeeded
rust-test Build rust-test has succeeded
2023-08-25 02:03:35 -04:00
Tom Alexander
fc79507ef3 Merge branch 'plain_list_perf_investigation'
All checks were successful
rustfmt Build rustfmt has succeeded
rust-test Build rust-test has succeeded
rust-build Build rust-build has succeeded
2023-08-25 01:10:04 -04:00
Tom Alexander
9c1e6ccc97 Add a detect_element function.
All checks were successful
rust-test Build rust-test has succeeded
rust-build Build rust-build has succeeded
This is an optimization. When you have something like plain text which ends when it hits the next element, we only need to parse enough to detect that an element is about to occur. For elements like plain lists, this is as simple as parsing a line starting with optional whitespace and then a bullet, which avoids parsing the entire plain list tree. The benefit is most noticeable in deeply nested plain lists.
2023-08-25 01:07:53 -04:00
Tom Alexander
0dbc8f0925 Remove redundant exit matcher checks.
All checks were successful
rust-test Build rust-test has succeeded
rust-build Build rust-build has succeeded
2023-08-25 00:01:56 -04:00
Tom Alexander
02fe10fba3 Move objects to a lower exit class.
All checks were successful
rust-test Build rust-test has succeeded
rust-build Build rust-build has succeeded
Paragraph's exit matcher which detects elements was causing the plain list parser to exit after the first item was parsed which was causing significant amounts of re-parsing.
2023-08-24 23:34:23 -04:00
Tom Alexander
33d7ae03d1 Add a TODO.
All checks were successful
rust-test Build rust-test has succeeded
rust-build Build rust-build has succeeded
2023-08-24 21:35:34 -04:00
Tom Alexander
03faa7257f Move the indent level for plain list's exit matcher to const fn instead of grabbing from the context.
All checks were successful
rust-test Build rust-test has succeeded
rust-build Build rust-build has succeeded
This made a slight improvement to performance.
2023-08-24 20:50:24 -04:00
Tom Alexander
ae3510abd5 Do not cast lesser block name to lowercase at runtime.
This reduced the runtime of my problematic test case from 6.9 seconds to 6 seconds.
2023-08-24 20:10:43 -04:00
fluxcdbot
ad3f47864a CI: autofix rust code.
Some checks failed
rustfmt Build rustfmt has succeeded
rust-test Build rust-test has succeeded
rust-build Build rust-build has failed
2023-08-24 23:43:41 +00:00
Tom Alexander
533ef2a9a8 Merge branch 'wrapped_input'
All checks were successful
rustfmt Build rustfmt has succeeded
rust-test Build rust-test has succeeded
rust-build Build rust-build has succeeded
2023-08-24 19:40:55 -04:00
Tom Alexander
cf37bc4111 Remove unnecessary context from some util functions.
All checks were successful
rust-test Build rust-test has succeeded
rust-build Build rust-build has succeeded
2023-08-24 19:29:00 -04:00
Tom Alexander
e5224cda63 Removing dead code.
All checks were successful
rust-test Build rust-test has succeeded
rust-build Build rust-build has succeeded
2023-08-24 18:40:25 -04:00
Tom Alexander
64e3481660 Update get_consumed to use the new wrapped input type. 2023-08-24 18:33:40 -04:00
Tom Alexander
32071ce74d Fix handling of start of line in OrgSource.
All checks were successful
rust-test Build rust-test has succeeded
rust-build Build rust-build has succeeded
2023-08-24 18:08:16 -04:00
Tom Alexander
e84e2b5147 Update tests to compile again. 2023-08-24 17:15:24 -04:00
Tom Alexander
3348807a05 Eliminate the document root context element. 2023-08-24 17:01:12 -04:00
Tom Alexander
720afa5d32 Update getting the previous character and previous line.
This can be done a lot more efficiently now that we are keeping track of this information in the wrapped input type instead of having to fetch to the original document out of the context tree.
2023-08-24 16:56:07 -04:00
Tom Alexander
dab598e5e7 Convert all functions to using the wrapped input type.
Some checks failed
rust-test Build rust-test has failed
rust-build Build rust-build has failed
2023-08-24 16:06:29 -04:00
Tom Alexander
b7a5dd48ea Impl missing traits. 2023-08-22 23:32:27 -04:00
Tom Alexander
c475dce6da Fix lifetime issue. 2023-08-22 23:14:23 -04:00
Tom Alexander
6d1675fa00 Lifetime issue. 2023-08-22 22:57:44 -04:00
Tom Alexander
cda49c628c Move the wrapped input into the parser. 2023-08-22 22:33:50 -04:00
Tom Alexander
65b87bd65d Merge remote-tracking branch 'input/main' into wrapped_input 2023-08-22 22:26:55 -04:00
Tom Alexander
5a7f34b63e Prepare for merging into Organic. 2023-08-22 22:24:35 -04:00
Tom Alexander
edff1e089d Implement text since line break. 2023-08-22 22:18:44 -04:00
Tom Alexander
bc29f1dfc0 Add slicing tests. 2023-08-22 21:38:50 -04:00
Tom Alexander
e4656cddf6 Implement slice, take, and compare. 2023-08-22 21:25:13 -04:00
Tom Alexander
1e3dadd458 Wrap the input. 2023-08-22 17:24:26 -04:00
Tom Alexander
2ec055af5a Very simple setup. 2023-08-22 17:22:13 -04:00
Tom Alexander
6823db5c60 Initial commit. 2023-08-22 17:11:45 -04:00
Tom Alexander
21e1ceb8e0 Merge branch 'add_performance_check_scripts'
All checks were successful
rustfmt Build rustfmt has succeeded
rust-test Build rust-test has succeeded
rust-build Build rust-build has succeeded
2023-08-22 14:21:44 -04:00
Tom Alexander
655af88cdf Add scripts for running perf and callgrind. 2023-08-22 14:21:27 -04:00
Tom Alexander
8561fdc1bd Make the autogen prefix fully integrated into the test name.
All checks were successful
rustfmt Build rustfmt has succeeded
rust-test Build rust-test has succeeded
rust-build Build rust-build has succeeded
2023-08-21 00:14:10 -04:00
Tom Alexander
f2089257b0 Re-enable disabled test.
All checks were successful
rustfmt Build rustfmt has succeeded
rust-build Build rust-build has succeeded
rust-test Build rust-test has succeeded
The latest code in org-mode has been fixed.
2023-08-21 00:08:26 -04:00
Tom Alexander
09821c8898 Prefix the automatically generated tests.
All checks were successful
rustfmt Build rustfmt has succeeded
rust-test Build rust-test has succeeded
rust-build Build rust-build has succeeded
2023-08-20 23:53:11 -04:00
Tom Alexander
69ecfd2646 Move all the specific-token tests into subfolders.
All checks were successful
rustfmt Build rustfmt has succeeded
rust-test Build rust-test has succeeded
rust-build Build rust-build has succeeded
2023-08-20 23:38:47 -04:00
Tom Alexander
8162f03051 Put all trailing whitespace ownership test cases into the automated tests.
All checks were successful
rustfmt Build rustfmt has succeeded
rust-test Build rust-test has succeeded
rust-build Build rust-build has succeeded
Notes for this investigation moved to cba1d1e988/notes/plain_list_ownership_notes.org .

Mailing list thread on the investigation: https://list.orgmode.org/9372527e-3852-419e-936a-7b4dd38cc847@app.fastmail.com/ .
2023-08-20 16:03:31 -04:00
Tom Alexander
d8c3285e3c Add --init flag to docker run.
All checks were successful
rustfmt Build rustfmt has succeeded
rust-build Build rust-build has succeeded
rust-test Build rust-test has succeeded
I noticed in a separate project that ctrl+c was not being honored under --init was passed, so I'm adding it in here.
2023-08-19 02:51:00 -04:00
Tom Alexander
5db6cd617e Improve test cases for plain list ownership. 2023-08-19 02:30:31 -04:00
Tom Alexander
4cd3697fb0 Update org-mode version in dockerfile. 2023-08-18 23:20:29 -04:00
Tom Alexander
2cd6f736c2 Fix building without compare feature.
All checks were successful
rustfmt Build rustfmt has succeeded
rust-test Build rust-test has succeeded
rust-build Build rust-build has succeeded
2023-08-17 00:13:25 -04:00
fluxcdbot
5686256039 CI: autofix rust code.
Some checks failed
rust-build Build rust-build has failed
rustfmt Build rustfmt has succeeded
rust-test Build rust-test has succeeded
2023-08-17 04:05:48 +00:00
Tom Alexander
7cf1b2d2b8 Disable the failing plain list whitespace ownership test.
Some checks failed
rust-build Build rust-build has failed
rustfmt Build rustfmt has succeeded
rust-test Build rust-test has succeeded
2023-08-17 00:03:05 -04:00
Tom Alexander
b848d7be73 Merge branch 'no_files' 2023-08-16 23:57:58 -04:00
Tom Alexander
74f4aa8d33 Remove dependency on files for running compare.
The tests still use files since they get the test name from a file but compare does the same action via stdin so it can operator on any org source.
2023-08-16 23:56:05 -04:00
Tom Alexander
4776898894 Merge branch 'fix_plain_list'
Some checks failed
rustfmt Build rustfmt has succeeded
rust-test Build rust-test has failed
rust-build Build rust-build has succeeded
2023-08-16 21:06:54 -04:00
Tom Alexander
8e95ce6368 Add notes about plain list trailing blank line ownership investigation. 2023-08-16 20:24:00 -04:00
Tom Alexander
6c9c304f37 Re-enable disabled test. 2023-08-16 17:39:10 -04:00
Tom Alexander
7fafbfb6bb Do not consume whitespace in the final plain list item. 2023-08-16 17:37:19 -04:00
Tom Alexander
56281633f3 Support blank link in plain_list_item_end, move exit matcher to end of loop in plain_list, and maybe consume trailing whitespace in plain_list_item. 2023-08-16 17:09:06 -04:00
Tom Alexander
823c33ef8e Reduce use of expect in main.rs 2023-08-16 16:37:14 -04:00
Tom Alexander
e5e5120a10 Move telemetry handling to the tracing-specific main function.
All checks were successful
rustfmt Build rustfmt has succeeded
rust-test Build rust-test has succeeded
rust-build Build rust-build has succeeded
This is so main_body can exit with an error at any time without missing the shutdown_telemetry function. This does not catch panics.
2023-08-16 16:05:24 -04:00
Tom Alexander
7df393f31d Make a new naive implementation of plain_list_item.
Still need to update plain_list_item_end and handle the whitespace ownership issues, but starting from a simplified state will help.
2023-08-16 16:05:24 -04:00
Tom Alexander
72d5f8f35c Make a new naive implementation of plain_list. 2023-08-16 16:05:24 -04:00
Tom Alexander
dae46adc12 Feature-gate tracing import.
All checks were successful
rustfmt Build rustfmt has succeeded
rust-test Build rust-test has succeeded
rust-build Build rust-build has succeeded
2023-08-14 23:33:38 -04:00
Tom Alexander
d0dc737c79 Merge branch 'plain_list_whitespace_ownership_issue'
Some checks failed
rust-build Build rust-build has failed
rustfmt Build rustfmt has succeeded
rust-test Build rust-test has succeeded
2023-08-14 23:20:37 -04:00
Tom Alexander
1c9877015d Disable the test showing my plain list implementation is broken. 2023-08-14 23:20:28 -04:00
Tom Alexander
2938d5809a Use the rust cache for make dockertest. 2023-08-14 23:17:38 -04:00
Tom Alexander
f7ec89858d Add notes about optimization ideas. 2023-08-14 23:16:23 -04:00
Tom Alexander
67b4dfdce6 Merge branch 'tracing_fixes'
Some checks failed
rust-build Build rust-build has failed
rustfmt Build rustfmt has succeeded
rust-test Build rust-test has succeeded
2023-08-14 22:12:05 -04:00
Tom Alexander
63d092c83d Group the two traces per compare into one trace. 2023-08-14 22:10:58 -04:00
Tom Alexander
a7b298eeec Fix lesser block exit priority.
The paragraph end was matching text inside lesser blocks.
2023-08-14 17:32:10 -04:00
Tom Alexander
1bbfbc3164 Add additional tracing to lesser block. 2023-08-14 17:32:09 -04:00
Tom Alexander
2bcc3f0599 Fix reporting of jaeger traces when diff does not match.
The early exit was causing some traces to not be reported.
2023-08-14 17:32:09 -04:00
Tom Alexander
b93a12c32c Add support for escaped double quotes in sexp. 2023-08-14 16:55:04 -04:00
Tom Alexander
df3045e424 Merge branch 'script_improvement'
All checks were successful
rustfmt Build rustfmt has succeeded
rust-test Build rust-test has succeeded
rust-build Build rust-build has succeeded
2023-08-14 16:13:30 -04:00
Tom Alexander
72b8fec1be Add support for tracing in run_docker_compare.bash. 2023-08-14 16:12:31 -04:00
Tom Alexander
ab17904b1c Clean up run_integration_test.bash. 2023-08-14 15:53:17 -04:00
Tom Alexander
306878c95d Clean up run_docker_integration_test.bash 2023-08-14 15:50:05 -04:00
Tom Alexander
5768c8acda Add a script to run compare using the docker image. 2023-08-14 15:30:13 -04:00
Tom Alexander
e28290ed79 Merge branch 'source_based_tests'
All checks were successful
rustfmt Build rustfmt has succeeded
rust-build Build rust-build has succeeded
rust-test Build rust-test has succeeded
2023-08-14 14:14:26 -04:00
Tom Alexander
fbabf60559 Add ignore to test export_snippet_paragraph_break_precedence. 2023-08-14 14:01:00 -04:00
Tom Alexander
92abac37e2 s/precedent/precedence/
I used the wrong word. This is referring to the priority between paragraphs ending vs export snippets ending, not a reference to something occurring in the past.
2023-08-14 13:57:01 -04:00
Tom Alexander
899073e54f Update to the latest org-mode. 2023-08-14 13:33:05 -04:00
Tom Alexander
eb379af78d Switch export snippet to use exit matchers. 2023-08-14 13:13:32 -04:00
Tom Alexander
422804d846 Add script for running specific tests inside docker.
Some checks failed
rust-build Build rust-build has failed
rust-test Build rust-test has failed
2023-08-14 12:21:15 -04:00
Tom Alexander
cc83431d62 Consume trailing whitespace for property drawers.
Some checks failed
rust-build Build rust-build has failed
rust-test Build rust-test has failed
This is a change between the org-mode in emacs 29.1 and the org-mode currently in main.
2023-08-14 11:57:12 -04:00
Tom Alexander
00354ccc20 Add a volume for cargo cache.
This is to be a good citizen by not downloading all the rust dependencies every time I run the tests locally. Unfortunately, it will still compile all the dependencies each time, but that is a local operation.
2023-08-14 10:57:48 -04:00
Tom Alexander
b75eed6b1e Enable tests that were disabled before.
Some checks are pending
rust-test Build rust-test has started
rust-build Build rust-build has succeeded
2023-08-13 02:21:02 -04:00
Tom Alexander
e33ec4a02c Add support for reading begin/end bounds in the new standard-properties format. 2023-08-13 02:21:02 -04:00
Tom Alexander
f7afcec824 Add support for hash notation in the elisp parser. 2023-08-13 02:21:02 -04:00
Tom Alexander
cf0991fdff Add support for parsing vectors in the elisp parser. 2023-08-13 02:21:02 -04:00
Tom Alexander
d1e0ee831c Switch to installing emacs and org-mode from source in test container.
This is to integrate fixes that have been committed to org-mode but have not made it into emacs, while also getting the latest emacs on alpine.
2023-08-13 02:21:01 -04:00
Tom Alexander
34985c9045 Add makefile target for running the tests inside the docker container.
Some checks failed
rust-build Build rust-build has failed
rust-test Build rust-test has failed
rustfmt Build rustfmt has succeeded
2023-08-13 02:20:16 -04:00
Tom Alexander
7da09fea74 Switch to specifying timeouts instead of timeout in tekton pipelinerun. 2023-08-13 02:20:16 -04:00
Tom Alexander
fc28e3b514 Add a test for trailing blank lines after paragraphs.
Some checks failed
rust-test Build rust-test has failed
rustfmt Build rustfmt has succeeded
rust-build Build rust-build has succeeded
The behavior in emacs does not match the description in the org-mode documentation. I have sent an email to the org-mode mailing list and I am waiting their response so I can adjust (or not adjust) my parser accordingly.
2023-08-11 01:37:04 -04:00
Tom Alexander
df5ee5af16 Explicitly list which files to include in the cargo package.
Some checks failed
rust-build Build rust-build has failed
rust-test Build rust-test has failed
rustfmt Build rustfmt has succeeded
We are including a bunch of files that are not needed for running the rust code. This excludes them to be a better citizen to both crates.io and all users of this package.
2023-08-11 00:11:54 -04:00
138 changed files with 2491 additions and 1257 deletions

View File

@@ -1,3 +1,4 @@
**/.git **/.git
target target
Cargo.lock Cargo.lock
notes/

View File

@@ -14,10 +14,6 @@ spec:
- name: path-to-dockerfile - name: path-to-dockerfile
description: The path to the Dockerfile description: The path to the Dockerfile
type: string type: string
- name: command
type: array
description: Command to run.
default: []
tasks: tasks:
- name: report-pending - name: report-pending
taskRef: taskRef:
@@ -81,9 +77,19 @@ spec:
workspace: docker-credentials workspace: docker-credentials
runAfter: runAfter:
- fetch-repository - fetch-repository
- name: run-image-none - name: build-organic
taskRef: taskRef:
name: run-docker-image name: run-docker-image
matrix:
params:
- name: feature-compare
value:
- "true"
- "false"
- name: feature-tracing
value:
- "true"
- "false"
workspaces: workspaces:
- name: source - name: source
workspace: git-source workspace: git-source
@@ -93,77 +99,21 @@ spec:
- build-image - build-image
params: params:
- name: command - name: command
value: ["$(params.command[*])"] value: ["/bin/sh", "-c"]
- name: args - name: args
value: ["--no-default-features"] value:
- name: docker-image - |
value: "$(params.image-name):$(tasks.fetch-repository.results.commit)" set -euo pipefail
- name: run-image-tracing IFS=$$'\n\t'
taskRef: features=()
name: run-docker-image if [ $(params.feature-compare) = "true" ]; then features+=(compare); fi
workspaces: if [ $(params.feature-tracing) = "true" ]; then features+=(tracing); fi
- name: source if [ $${#features[@]} -eq 0 ]; then
workspace: git-source exec cargo build --no-default-features
- name: cargo-cache else
workspace: cargo-cache featurelist=$$(IFS="," ; echo "$${features[*]}")
runAfter: exec cargo build --no-default-features --features "$$featurelist"
- run-image-none fi
params:
- name: command
value: ["$(params.command[*])"]
- name: args
value: ["--no-default-features", "--features", "tracing"]
- name: docker-image
value: "$(params.image-name):$(tasks.fetch-repository.results.commit)"
- name: run-image-compare
taskRef:
name: run-docker-image
workspaces:
- name: source
workspace: git-source
- name: cargo-cache
workspace: cargo-cache
runAfter:
- run-image-tracing
params:
- name: command
value: ["$(params.command[*])"]
- name: args
value: ["--no-default-features", "--features", "compare"]
- name: docker-image
value: "$(params.image-name):$(tasks.fetch-repository.results.commit)"
- name: run-image-default
taskRef:
name: run-docker-image
workspaces:
- name: source
workspace: git-source
- name: cargo-cache
workspace: cargo-cache
runAfter:
- run-image-compare
params:
- name: command
value: ["$(params.command[*])"]
- name: args
value: []
- name: docker-image
value: "$(params.image-name):$(tasks.fetch-repository.results.commit)"
- name: run-image-all
taskRef:
name: run-docker-image
workspaces:
- name: source
workspace: git-source
- name: cargo-cache
workspace: cargo-cache
runAfter:
- run-image-default
params:
- name: command
value: ["$(params.command[*])"]
- name: args
value: ["--no-default-features", "--features", "tracing,compare"]
- name: docker-image - name: docker-image
value: "$(params.image-name):$(tasks.fetch-repository.results.commit)" value: "$(params.image-name):$(tasks.fetch-repository.results.commit)"
finally: finally:
@@ -256,5 +206,3 @@ spec:
value: docker/organic_build/ value: docker/organic_build/
- name: path-to-dockerfile - name: path-to-dockerfile
value: docker/organic_build/Dockerfile value: docker/organic_build/Dockerfile
- name: command
value: [cargo, build]

View File

@@ -4,6 +4,10 @@ metadata:
name: rust-test name: rust-test
spec: spec:
pipelineSpec: pipelineSpec:
timeouts:
pipeline: "2h0m0s"
tasks: "1h0m40s"
finally: "0h30m0s"
params: params:
- name: image-name - name: image-name
description: The name for the built image description: The name for the built image
@@ -201,7 +205,6 @@ spec:
secret: secret:
secretName: harbor-plain secretName: harbor-plain
serviceAccountName: build-bot serviceAccountName: build-bot
timeout: 240h0m0s
params: params:
- name: image-name - name: image-name
value: "harbor.fizz.buzz/private/organic-test" value: "harbor.fizz.buzz/private/organic-test"

View File

@@ -10,6 +10,12 @@ readme = "README.md"
keywords = ["emacs", "org-mode"] keywords = ["emacs", "org-mode"]
categories = ["parsing"] categories = ["parsing"]
resolver = "2" resolver = "2"
include = [
"LICENSE",
"**/*.rs",
"Cargo.toml",
"tests/*"
]
[lib] [lib]
name = "organic" name = "organic"
@@ -34,10 +40,16 @@ tracing-subscriber = { version = "0.3.17", optional = true, features = ["env-fil
walkdir = "2.3.3" walkdir = "2.3.3"
[features] [features]
default = ["compare", "tracing"] default = ["compare"]
compare = [] compare = []
tracing = ["dep:opentelemetry", "dep:opentelemetry-otlp", "dep:opentelemetry-semantic-conventions", "dep:tokio", "dep:tracing", "dep:tracing-opentelemetry", "dep:tracing-subscriber"] tracing = ["dep:opentelemetry", "dep:opentelemetry-otlp", "dep:opentelemetry-semantic-conventions", "dep:tokio", "dep:tracing", "dep:tracing-opentelemetry", "dep:tracing-subscriber"]
[profile.release] [profile.release-lto]
inherits = "release"
lto = true lto = true
strip = "symbols" strip = "symbols"
[profile.perf]
inherits = "release"
lto = true
debug = true

View File

@@ -35,7 +35,17 @@ clean:
.PHONY: test .PHONY: test
test: test:
> cargo test --lib --test test_loader -- --test-threads $(TESTJOBS) > cargo test --no-fail-fast --lib --test test_loader -- --test-threads $(TESTJOBS)
.PHONY: dockertest
dockertest:
> $(MAKE) -C docker/organic_test
> docker run --init --rm -i -t -v "$$(readlink -f ./):/source:ro" --mount source=cargo-cache,target=/usr/local/cargo/registry --mount source=rust-cache,target=/target --env CARGO_TARGET_DIR=/target -w /source organic-test cargo test --no-fail-fast --lib --test test_loader -- --test-threads $(TESTJOBS)
.PHONY: dockerclean
dockerclean:
# Delete volumes created for running the tests in docker. This does not touch anything related to the jaeger docker container.
> docker volume rm cargo-cache rust-cache
.PHONY: integrationtest .PHONY: integrationtest
integrationtest: integrationtest:
@@ -49,8 +59,8 @@ unittest:
jaeger: jaeger:
# 4317 for OTLP gRPC, 4318 for OTLP HTTP. We currently use gRPC but I forward both ports regardless. # 4317 for OTLP gRPC, 4318 for OTLP HTTP. We currently use gRPC but I forward both ports regardless.
# #
# These flags didn't help even though they seem like they would: --collector.otlp.grpc.max-message-size=10000000 --collector.queue-size=20000 --collector.num-workers=100 # These flags didn't help even though they seem like they would: --collector.queue-size=20000 --collector.num-workers=100
> docker run -d --rm --name organicdocker -p 4317:4317 -p 4318:4318 -p 16686:16686 -e COLLECTOR_OTLP_ENABLED=true jaegertracing/all-in-one:1.47 --collector.grpc-server.max-message-size=10000000 > docker run -d --rm --name organicdocker -p 4317:4317 -p 4318:4318 -p 16686:16686 -e COLLECTOR_OTLP_ENABLED=true jaegertracing/all-in-one:1.47 --collector.grpc-server.max-message-size=20000000 --collector.otlp.grpc.max-message-size=20000000
.PHONY: jaegerweb .PHONY: jaegerweb
jaegerweb: jaegerweb:

View File

@@ -41,6 +41,7 @@ fn write_test(test_file: &mut File, test: &walkdir::DirEntry) {
.strip_suffix(".org") .strip_suffix(".org")
.expect("Should have .org extension") .expect("Should have .org extension")
.replace("/", "_"); .replace("/", "_");
let test_name = format!("autogen_{}", test_name);
if let Some(_reason) = is_expect_fail(test_name.as_str()) { if let Some(_reason) = is_expect_fail(test_name.as_str()) {
write!(test_file, "#[ignore]\n").unwrap(); write!(test_file, "#[ignore]\n").unwrap();
@@ -71,10 +72,9 @@ use organic::parser::sexp::sexp_with_padding;
fn is_expect_fail(name: &str) -> Option<&str> { fn is_expect_fail(name: &str) -> Option<&str> {
match name { match name {
"drawer_drawer_with_headline_inside" => Some("Apparently lines with :end: become their own paragraph. This odd behavior needs to be investigated more."), "autogen_greater_element_drawer_drawer_with_headline_inside" => Some("Apparently lines with :end: become their own paragraph. This odd behavior needs to be investigated more."),
"element_container_priority_footnote_definition_dynamic_block" => Some("Apparently broken begin lines become their own paragraph."), "autogen_element_container_priority_footnote_definition_dynamic_block" => Some("Apparently broken begin lines become their own paragraph."),
"paragraphs_paragraph_with_backslash_line_breaks" => Some("The text we're getting out of the parse tree is already processed to remove line breaks, so our comparison needs to take that into account."), "autogen_lesser_element_paragraphs_paragraph_with_backslash_line_breaks" => Some("The text we're getting out of the parse tree is already processed to remove line breaks, so our comparison needs to take that into account."),
"export_snippet_paragraph_break_precedent" => Some("Emacs 28 has broken behavior so the tests in the CI fail."),
_ => None, _ => None,
} }
} }

View File

@@ -6,7 +6,7 @@ all: build push
.PHONY: build .PHONY: build
build: build:
docker build -t $(IMAGE_NAME) -f Dockerfile ../ docker build -t $(IMAGE_NAME) -f Dockerfile ../../
.PHONY: push .PHONY: push
push: push:

View File

@@ -6,7 +6,7 @@ all: build push
.PHONY: build .PHONY: build
build: build:
docker build -t $(IMAGE_NAME) -f Dockerfile ../ docker build -t $(IMAGE_NAME) -f Dockerfile ../../
.PHONY: push .PHONY: push
push: push:

View File

@@ -1,4 +1,32 @@
FROM rustlang/rust:nightly-alpine3.17 FROM alpine:3.17 AS build
RUN apk add --no-cache build-base musl-dev git autoconf make texinfo gnutls-dev ncurses-dev gawk
RUN apk add --no-cache musl-dev emacs
FROM build AS build-emacs
ARG EMACS_VERSION=emacs-29.1
RUN git clone --depth 1 --branch $EMACS_VERSION https://git.savannah.gnu.org/git/emacs.git /root/emacs
WORKDIR /root/emacs
RUN mkdir /root/dist
RUN ./autogen.sh
RUN ./configure --prefix /usr --without-x --without-sound
RUN make
RUN make DESTDIR="/root/dist" install
FROM build AS build-org-mode
ARG ORG_VERSION=7bdec435ff5d86220d13c431e799c5ed44a57da1
COPY --from=build-emacs /root/dist/ /
RUN mkdir /root/dist
# Savannah does not allow fetching specific revisions, so we're going to have to put unnecessary load on their server by cloning main and then checking out the revision we want.
RUN git clone https://git.savannah.gnu.org/git/emacs/org-mode.git /root/org-mode && git -C /root/org-mode checkout $ORG_VERSION
# RUN mkdir /root/org-mode && git -C /root/org-mode init --initial-branch=main && git -C /root/org-mode remote add origin https://git.savannah.gnu.org/git/emacs/org-mode.git && git -C /root/org-mode fetch origin $ORG_VERSION && git -C /root/org-mode checkout FETCH_HEAD
WORKDIR /root/org-mode
RUN make compile
RUN make DESTDIR="/root/dist" install
FROM rustlang/rust:nightly-alpine3.17
RUN apk add --no-cache musl-dev ncurses gnutls
RUN cargo install --locked --no-default-features --features ci-autoclean cargo-cache RUN cargo install --locked --no-default-features --features ci-autoclean cargo-cache
COPY --from=build-emacs /root/dist/ /
COPY --from=build-org-mode /root/dist/ /

View File

@@ -6,7 +6,7 @@ all: build push
.PHONY: build .PHONY: build
build: build:
docker build -t $(IMAGE_NAME) -f Dockerfile ../ docker build -t $(IMAGE_NAME) -f Dockerfile ../../
.PHONY: push .PHONY: push
push: push:

0
foo Normal file
View File

View File

@@ -0,0 +1,27 @@
* Analysis
** Parse start per character
It might help analysis to record how often we start a specific type of parse for each character. For example, at the start of a plain list, if we had a count of how often each character was the start of a parse of a list we could use that to see how often that list is getting re-parsed.
* Optimizations
** Edit whitespace for list items
Whether or not a list item owns the trailing whitespace depends on if it is the last list item in that list. Since we do not know ahead of time if an item is the last item in the list, we have to either re-parse the list item or modify it after parsing.
*** For
We already are modifying the source of some elements after-the-fact with src_rust{set_source()} so this would be more of the same.
*** Against
I'd like to phase out such modifications because they seem hacky and fragile.
** Make detect element function
Some exit matchers are based on when the next element is found. Some elements do not need to be fully parsed to identify that they are a valid element. For example, src_org{1. foo} can already be identified as the start of a plain list (in the right context) without needing to parse the entire element.
*** For
Avoiding parsing the entire element for an exit matcher would reduce redundant parses.
*** Against
This adds code complexity and introduces the potential for bugs.
How many elements can be reasonably early-detected? For example, src_org{#+begin_src foo} is not enough to detect the start of a source block because without the src_org{#+end_src} it is just plain text.
** Grab multiple characters in plaintext parser before checking exit matcher
Currently we check the exit matcher after each character inside the plain text parser (and many others). Are there character sequences we can assume no exit matcher will trigger between? For example, a contiguous string of latin-alphabet letters?
*** For
This could significantly reduce our calls to exit matchers.
*** Against
I think targets would break this.
The exit matchers are already implicitly building this behavior since they should all exit very early when the starting character is wrong. Putting this logic in a centralized place, far away from where those characters are actually going to be used, is unfortunate for readability.

View File

@@ -0,0 +1,9 @@
1. foo
1. bar
2. baz
2. lorem
ipsum

View File

@@ -0,0 +1,11 @@
1. foo
1. bar
2. baz
cat
2. lorem
ipsum

View File

@@ -0,0 +1,10 @@
1. cat
1. foo
1. bar
2. baz
2. lorem
ipsum

View File

@@ -0,0 +1,12 @@
1. cat
1. foo
1. bar
2. baz
2. lorem
2. dog
ipsum

View File

@@ -0,0 +1,18 @@
foo bar.
* Lorem
baz
* Ipsum
alpha
beta

13
scripts/callgrind.bash Executable file
View File

@@ -0,0 +1,13 @@
#!/usr/bin/env bash
#
set -euo pipefail
IFS=$'\n\t'
DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
cd "$DIR/../"
RUSTFLAGS="-C opt-level=0" cargo build --no-default-features
valgrind --tool=callgrind --callgrind-out-file=callgrind.out target/debug/compare
echo "You probably want to run:"
echo "callgrind_annotate --auto=yes callgrind.out"

18
scripts/perf.bash Executable file
View File

@@ -0,0 +1,18 @@
#!/usr/bin/env bash
#
set -euo pipefail
IFS=$'\n\t'
DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
: ${PROFILE:="perf"}
cd "$DIR/../"
cargo build --profile "$PROFILE" --no-default-features
perf record --freq=2000 --call-graph dwarf --output=perf.data target/${PROFILE}/compare
# Convert to a format firefox will read
# flags to consider --show-info
perf script -F +pid --input perf.data > perf.firefox
echo "You probably want to go to https://profiler.firefox.com/"
echo "Either that or run hotspot"

46
scripts/run_docker_compare.bash Executable file
View File

@@ -0,0 +1,46 @@
#!/usr/bin/env bash
#
set -euo pipefail
IFS=$'\n\t'
DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
: ${SHELL:="NO"} # or YES to launch a shell instead of running the test
: ${TRACE:="NO"} # or YES to send traces to jaeger
: ${BACKTRACE:="NO"} # or YES to print a rust backtrace when panicking
cd "$DIR/../"
REALPATH=$(command -v uu-realpath || command -v realpath)
MAKE=$(command -v gmake || command -v make)
function main {
build_container
launch_container
}
function build_container {
$MAKE -C "$DIR/../docker/organic_test"
}
function launch_container {
local additional_flags=()
local additional_args=()
if [ "$SHELL" != "YES" ]; then
additional_args+=(cargo run)
else
additional_flags+=(-t)
fi
if [ "$TRACE" = "YES" ]; then
# We use the host network so it can talk to jaeger hosted at 127.0.0.1
additional_flags+=(--network=host --env RUST_LOG=debug)
fi
if [ "$BACKTRACE" = "YES" ]; then
additional_flags+=(--env RUST_BACKTRACE=full)
fi
docker run "${additional_flags[@]}" --init --rm -i -v "$($REALPATH ./):/source:ro" --mount source=cargo-cache,target=/usr/local/cargo/registry --mount source=rust-cache,target=/target --env CARGO_TARGET_DIR=/target -w /source organic-test "${additional_args[@]}"
}
main "${@}"

View File

@@ -0,0 +1,57 @@
#!/usr/bin/env bash
#
set -euo pipefail
IFS=$'\n\t'
DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
cd "$DIR/../"
REALPATH=$(command -v uu-realpath || command -v realpath)
MAKE=$(command -v gmake || command -v make)
function main {
local test_names=$(get_test_names "${@}")
build_container
local test
while read test; do
launch_container "$test"
done<<<"$test_names"
}
function build_container {
$MAKE -C "$DIR/../docker/organic_test"
}
function get_test_names {
local test_file
local samples_dir=$($REALPATH "$DIR/../org_mode_samples")
for test_file in "$@"
do
if [ -e "$test_file" ]; then
local test_file_full_path=$($REALPATH "$test_file")
local relative_to_samples=$($REALPATH --relative-to "$samples_dir" "$test_file_full_path")
local without_extension="${relative_to_samples%.org}"
echo "${without_extension/\//_}" | tr '[:upper:]' '[:lower:]'
else
echo "$test_file" | tr '[:upper:]' '[:lower:]'
fi
done
}
function launch_container {
local test="$1"
local additional_args=()
local init_script=$(cat <<EOF
set -euo pipefail
IFS=\$'\n\t'
cargo test --no-fail-fast --lib --test test_loader "$test" -- --show-output
EOF
)
docker run --init --rm -v "$($REALPATH ./):/source:ro" --mount source=cargo-cache,target=/usr/local/cargo/registry --mount source=rust-cache,target=/target --env CARGO_TARGET_DIR=/target -w /source organic-test sh -c "$init_script"
}
main "${@}"

View File

@@ -4,17 +4,27 @@ set -euo pipefail
IFS=$'\n\t' IFS=$'\n\t'
DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )" DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
cd "$DIR/../"
REALPATH=$(command -v uu-realpath || command -v realpath) REALPATH=$(command -v uu-realpath || command -v realpath)
samples_dir=$(readlink -f "$DIR/../org_mode_samples") function main {
local test_names=$(get_test_names "${@}")
local test
while read test; do
cargo test --no-fail-fast --test test_loader "$test" -- --show-output
done<<<"$test_names"
}
function get_test_names { function get_test_names {
local test_file
local samples_dir=$($REALPATH "$DIR/../org_mode_samples")
for test_file in "$@" for test_file in "$@"
do do
if [ -e "$test_file" ]; then if [ -e "$test_file" ]; then
test_file_full_path=$(readlink -f "$test_file") local test_file_full_path=$($REALPATH "$test_file")
relative_to_samples=$($REALPATH --relative-to "$samples_dir" "$test_file_full_path") local relative_to_samples=$($REALPATH --relative-to "$samples_dir" "$test_file_full_path")
without_extension="${relative_to_samples%.org}" local without_extension="${relative_to_samples%.org}"
echo "${without_extension/\//_}" | tr '[:upper:]' '[:lower:]' echo "${without_extension/\//_}" | tr '[:upper:]' '[:lower:]'
else else
echo "$test_file" | tr '[:upper:]' '[:lower:]' echo "$test_file" | tr '[:upper:]' '[:lower:]'
@@ -22,6 +32,4 @@ function get_test_names {
done done
} }
get_test_names "$@" | while read test; do main "${@}"
(cd "$DIR/../" && cargo test --no-fail-fast --test test_loader "$test" -- --show-output)
done

View File

@@ -3,3 +3,5 @@ mod parse;
mod util; mod util;
pub use diff::compare_document; pub use diff::compare_document;
pub use parse::emacs_parse_org_document; pub use parse::emacs_parse_org_document;
pub use parse::get_emacs_version;
pub use parse::get_org_mode_version;

View File

@@ -1,22 +1,25 @@
use std::path::Path;
use std::process::Command; use std::process::Command;
pub fn emacs_parse_org_document<'a, C>(file_path: C) -> Result<String, Box<dyn std::error::Error>> pub fn emacs_parse_org_document<C>(file_contents: C) -> Result<String, Box<dyn std::error::Error>>
where where
C: AsRef<Path>, C: AsRef<str>,
{ {
let elisp_script = r#"(progn let escaped_file_contents = escape_elisp_string(file_contents);
let elisp_script = format!(
r#"(progn
(erase-buffer)
(insert "{escaped_file_contents}")
(org-mode) (org-mode)
(message "%s" (pp-to-string (org-element-parse-buffer))) (message "%s" (pp-to-string (org-element-parse-buffer)))
)"#; )"#,
escaped_file_contents = escaped_file_contents
);
let mut cmd = Command::new("emacs"); let mut cmd = Command::new("emacs");
let proc = cmd let proc = cmd
.arg("-q") .arg("-q")
.arg("--no-site-file") .arg("--no-site-file")
.arg("--no-splash") .arg("--no-splash")
.arg("--batch") .arg("--batch")
.arg("--insert")
.arg(file_path.as_ref().as_os_str())
.arg("--eval") .arg("--eval")
.arg(elisp_script); .arg(elisp_script);
let out = proc.output()?; let out = proc.output()?;
@@ -24,3 +27,62 @@ where
let org_sexp = out.stderr; let org_sexp = out.stderr;
Ok(String::from_utf8(org_sexp)?) Ok(String::from_utf8(org_sexp)?)
} }
fn escape_elisp_string<C>(file_contents: C) -> String
where
C: AsRef<str>,
{
let source = file_contents.as_ref();
let source_len = source.len();
// We allocate a string 10% larger than the source to account for escape characters. Without this, we would have more allocations during processing.
let mut output = String::with_capacity(source_len + (source_len / 10));
for c in source.chars() {
match c {
'"' | '\\' => {
output.push('\\');
output.push(c);
}
_ => {
output.push(c);
}
}
}
output
}
pub fn get_emacs_version() -> Result<String, Box<dyn std::error::Error>> {
let elisp_script = r#"(progn
(message "%s" (version))
)"#;
let mut cmd = Command::new("emacs");
let proc = cmd
.arg("-q")
.arg("--no-site-file")
.arg("--no-splash")
.arg("--batch")
.arg("--eval")
.arg(elisp_script);
let out = proc.output()?;
out.status.exit_ok()?;
Ok(String::from_utf8(out.stderr)?)
}
pub fn get_org_mode_version() -> Result<String, Box<dyn std::error::Error>> {
let elisp_script = r#"(progn
(org-mode)
(message "%s" (org-version nil t nil))
)"#;
let mut cmd = Command::new("emacs");
let proc = cmd
.arg("-q")
.arg("--no-site-file")
.arg("--no-splash")
.arg("--batch")
.arg("--eval")
.arg(elisp_script);
let out = proc.output()?;
out.status.exit_ok()?;
Ok(String::from_utf8(out.stderr)?)
}

View File

@@ -42,24 +42,100 @@ pub fn assert_bounds<'s, S: Source<'s>>(
emacs: &'s Token<'s>, emacs: &'s Token<'s>,
rust: &'s S, rust: &'s S,
) -> Result<(), Box<dyn std::error::Error>> { ) -> Result<(), Box<dyn std::error::Error>> {
let standard_properties = get_standard_properties(emacs)?;
let (begin, end) = (
standard_properties
.begin
.ok_or("Token should have a begin.")?,
standard_properties
.end
.ok_or("Token should have a begin.")?,
);
let (rust_begin, rust_end) = get_offsets(source, rust);
if (rust_begin + 1) != begin || (rust_end + 1) != end {
Err(format!("Rust bounds ({rust_begin}, {rust_end}) do not match emacs bounds ({emacs_begin}, {emacs_end})", rust_begin = rust_begin + 1, rust_end = rust_end + 1, emacs_begin=begin, emacs_end=end))?;
}
Ok(())
}
struct StandardProperties {
begin: Option<usize>,
#[allow(dead_code)]
post_affiliated: Option<usize>,
#[allow(dead_code)]
contents_begin: Option<usize>,
#[allow(dead_code)]
contents_end: Option<usize>,
end: Option<usize>,
#[allow(dead_code)]
post_blank: Option<usize>,
}
fn get_standard_properties<'s>(
emacs: &'s Token<'s>,
) -> Result<StandardProperties, Box<dyn std::error::Error>> {
let children = emacs.as_list()?; let children = emacs.as_list()?;
let attributes_child = children let attributes_child = children
.iter() .iter()
.nth(1) .nth(1)
.ok_or("Should have an attributes child.")?; .ok_or("Should have an attributes child.")?;
let attributes_map = attributes_child.as_map()?; let attributes_map = attributes_child.as_map()?;
let begin = attributes_map let standard_properties = attributes_map.get(":standard-properties");
.get(":begin") Ok(if standard_properties.is_some() {
.ok_or("Missing :begin attribute.")? let mut std_props = standard_properties
.as_atom()?; .expect("if statement proves its Some")
let end = attributes_map .as_vector()?
.get(":end") .into_iter();
.ok_or("Missing :end attribute.")? let begin = maybe_token_to_usize(std_props.next())?;
.as_atom()?; let post_affiliated = maybe_token_to_usize(std_props.next())?;
let (rust_begin, rust_end) = get_offsets(source, rust); let contents_begin = maybe_token_to_usize(std_props.next())?;
if (rust_begin + 1).to_string() != begin || (rust_end + 1).to_string() != end { let contents_end = maybe_token_to_usize(std_props.next())?;
Err(format!("Rust bounds ({rust_begin}, {rust_end}) do not match emacs bounds ({emacs_begin}, {emacs_end})", rust_begin = rust_begin + 1, rust_end = rust_end + 1, emacs_begin=begin, emacs_end=end))?; let end = maybe_token_to_usize(std_props.next())?;
let post_blank = maybe_token_to_usize(std_props.next())?;
StandardProperties {
begin,
post_affiliated,
contents_begin,
contents_end,
end,
post_blank,
}
} else {
let begin = maybe_token_to_usize(attributes_map.get(":begin").map(|token| *token))?;
let end = maybe_token_to_usize(attributes_map.get(":end").map(|token| *token))?;
let contents_begin =
maybe_token_to_usize(attributes_map.get(":contents-begin").map(|token| *token))?;
let contents_end =
maybe_token_to_usize(attributes_map.get(":contents-end").map(|token| *token))?;
let post_blank =
maybe_token_to_usize(attributes_map.get(":post-blank").map(|token| *token))?;
let post_affiliated =
maybe_token_to_usize(attributes_map.get(":post-affiliated").map(|token| *token))?;
StandardProperties {
begin,
post_affiliated,
contents_begin,
contents_end,
end,
post_blank,
}
})
} }
Ok(()) fn maybe_token_to_usize(
token: Option<&Token<'_>>,
) -> Result<Option<usize>, Box<dyn std::error::Error>> {
Ok(token
.map(|token| token.as_atom())
.map_or(Ok(None), |r| r.map(Some))?
.map(|val| {
if val == "nil" {
None
} else {
Some(val.parse::<usize>())
}
})
.flatten() // Outer option is whether or not the param exists, inner option is whether or not it is nil
.map_or(Ok(None), |r| r.map(Some))?)
} }

View File

@@ -4,6 +4,7 @@ use nom::IResult;
pub type Res<T, U> = IResult<T, U, CustomError<T>>; pub type Res<T, U> = IResult<T, U, CustomError<T>>;
// TODO: MyError probably shouldn't be based on the same type as the input type since it's used exclusively with static strings right now.
#[derive(Debug, PartialEq)] #[derive(Debug, PartialEq)]
pub enum CustomError<I> { pub enum CustomError<I> {
MyError(MyError<I>), MyError(MyError<I>),

View File

@@ -15,7 +15,8 @@ pub fn init_telemetry() -> Result<(), Box<dyn std::error::Error>> {
// TODO: I think the endpoint can be controlled by the OTEL_EXPORTER_OTLP_TRACES_ENDPOINT env variable instead of hard-coded into this code base. Regardless, I am the only developer right now so I am not too concerned. // TODO: I think the endpoint can be controlled by the OTEL_EXPORTER_OTLP_TRACES_ENDPOINT env variable instead of hard-coded into this code base. Regardless, I am the only developer right now so I am not too concerned.
let exporter = opentelemetry_otlp::new_exporter() let exporter = opentelemetry_otlp::new_exporter()
.tonic() .tonic()
.with_endpoint("http://localhost:4317/v1/traces"); // Using "localhost" is broken inside the docker container when tracing
.with_endpoint("http://127.0.0.1:4317/v1/traces");
let tracer = opentelemetry_otlp::new_pipeline() let tracer = opentelemetry_otlp::new_pipeline()
.tracing() .tracing()

View File

@@ -7,6 +7,10 @@ mod compare;
pub use compare::compare_document; pub use compare::compare_document;
#[cfg(feature = "compare")] #[cfg(feature = "compare")]
pub use compare::emacs_parse_org_document; pub use compare::emacs_parse_org_document;
#[cfg(feature = "compare")]
pub use compare::get_emacs_version;
#[cfg(feature = "compare")]
pub use compare::get_org_mode_version;
mod error; mod error;
pub mod parser; pub mod parser;

View File

@@ -1,17 +1,23 @@
#![feature(round_char_boundary)] #![feature(round_char_boundary)]
use std::path::Path; use std::io::Read;
#[cfg(feature = "compare")]
use ::organic::parser::document; use ::organic::parser::document;
#[cfg(feature = "compare")] #[cfg(feature = "compare")]
use organic::compare_document; use organic::compare_document;
#[cfg(feature = "compare")] #[cfg(feature = "compare")]
use organic::emacs_parse_org_document; use organic::emacs_parse_org_document;
#[cfg(feature = "compare")] #[cfg(feature = "compare")]
use organic::get_emacs_version;
#[cfg(feature = "compare")]
use organic::get_org_mode_version;
#[cfg(feature = "compare")]
use organic::parser::sexp::sexp_with_padding; use organic::parser::sexp::sexp_with_padding;
#[cfg(feature = "tracing")]
use crate::init_tracing::init_telemetry; use crate::init_tracing::init_telemetry;
#[cfg(feature = "tracing")]
use crate::init_tracing::shutdown_telemetry; use crate::init_tracing::shutdown_telemetry;
#[cfg(feature = "tracing")]
mod init_tracing; mod init_tracing;
#[cfg(not(feature = "tracing"))] #[cfg(not(feature = "tracing"))]
@@ -22,40 +28,47 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
#[cfg(feature = "tracing")] #[cfg(feature = "tracing")]
fn main() -> Result<(), Box<dyn std::error::Error>> { fn main() -> Result<(), Box<dyn std::error::Error>> {
let rt = tokio::runtime::Runtime::new()?; let rt = tokio::runtime::Runtime::new()?;
let result = rt.block_on(async { main_body() }); let result = rt.block_on(async {
init_telemetry()?;
let main_body_result = main_body();
shutdown_telemetry()?;
main_body_result
});
result result
} }
#[cfg_attr(feature = "tracing", tracing::instrument(ret, level = "debug"))]
fn main_body() -> Result<(), Box<dyn std::error::Error>> { fn main_body() -> Result<(), Box<dyn std::error::Error>> {
init_telemetry()?; let org_contents = read_stdin_to_string()?;
run_compare( run_compare(org_contents)
std::env::args() }
.nth(1)
.expect("Pass a single file into this script."), fn read_stdin_to_string() -> Result<String, Box<dyn std::error::Error>> {
)?; let mut stdin_contents = String::new();
shutdown_telemetry()?; std::io::stdin()
Ok(()) .lock()
.read_to_string(&mut stdin_contents)?;
Ok(stdin_contents)
} }
#[cfg(feature = "compare")] #[cfg(feature = "compare")]
fn run_compare<P: AsRef<Path>>(todo_org_path: P) -> Result<(), Box<dyn std::error::Error>> { fn run_compare<P: AsRef<str>>(org_contents: P) -> Result<(), Box<dyn std::error::Error>> {
let org_contents = std::fs::read_to_string(todo_org_path.as_ref()).expect("Read org file."); let emacs_version = get_emacs_version()?;
let (remaining, rust_parsed) = document(org_contents.as_str()).expect("Org Parse failure"); let org_mode_version = get_org_mode_version()?;
let org_sexp = eprintln!("Using emacs version: {}", emacs_version.trim());
emacs_parse_org_document(todo_org_path.as_ref()).expect("Use emacs to parse org file."); eprintln!("Using org-mode version: {}", org_mode_version.trim());
let (remaining, rust_parsed) = document(org_contents.as_ref()).map_err(|e| e.to_string())?;
let org_sexp = emacs_parse_org_document(org_contents.as_ref())?;
let (_remaining, parsed_sexp) = let (_remaining, parsed_sexp) =
sexp_with_padding(org_sexp.as_str()).expect("Sexp Parse failure"); sexp_with_padding(org_sexp.as_str()).map_err(|e| e.to_string())?;
println!("{}\n\n\n", org_contents.as_str()); println!("{}\n\n\n", org_contents.as_ref());
println!("{}", org_sexp); println!("{}", org_sexp);
println!("{:#?}", rust_parsed); println!("{:#?}", rust_parsed);
// We do the diffing after printing out both parsed forms in case the diffing panics // We do the diffing after printing out both parsed forms in case the diffing panics
let diff_result = let diff_result = compare_document(&parsed_sexp, &rust_parsed)?;
compare_document(&parsed_sexp, &rust_parsed).expect("Compare parsed documents."); diff_result.print()?;
diff_result
.print()
.expect("Print document parse tree diff.");
if diff_result.is_bad() { if diff_result.is_bad() {
Err("Diff results do not match.")?; Err("Diff results do not match.")?;
@@ -68,7 +81,11 @@ fn run_compare<P: AsRef<Path>>(todo_org_path: P) -> Result<(), Box<dyn std::erro
} }
#[cfg(not(feature = "compare"))] #[cfg(not(feature = "compare"))]
fn run_compare<P: AsRef<Path>>(_todo_org_path: P) -> Result<(), Box<dyn std::error::Error>> { fn run_compare<P: AsRef<str>>(org_contents: P) -> Result<(), Box<dyn std::error::Error>> {
println!("This program was built with compare disabled. Doing nothing."); eprintln!(
"This program was built with compare disabled. Only parsing with organic, not comparing."
);
let (remaining, rust_parsed) = document(org_contents.as_ref()).map_err(|e| e.to_string())?;
println!("{:#?}", rust_parsed);
Ok(()) Ok(())
} }

View File

@@ -4,6 +4,7 @@ use nom::combinator::peek;
use nom::combinator::recognize; use nom::combinator::recognize;
use nom::multi::many_till; use nom::multi::many_till;
use super::org_source::OrgSource;
use super::Context; use super::Context;
use crate::error::Res; use crate::error::Res;
use crate::parser::exiting::ExitClass; use crate::parser::exiting::ExitClass;
@@ -16,7 +17,10 @@ use crate::parser::util::get_consumed;
use crate::parser::AngleLink; use crate::parser::AngleLink;
#[cfg_attr(feature = "tracing", tracing::instrument(ret, level = "debug"))] #[cfg_attr(feature = "tracing", tracing::instrument(ret, level = "debug"))]
pub fn angle_link<'r, 's>(context: Context<'r, 's>, input: &'s str) -> Res<&'s str, AngleLink<'s>> { pub fn angle_link<'r, 's>(
context: Context<'r, 's>,
input: OrgSource<'s>,
) -> Res<OrgSource<'s>, AngleLink<'s>> {
let (remaining, _) = tag("<")(input)?; let (remaining, _) = tag("<")(input)?;
let (remaining, proto) = protocol(context, remaining)?; let (remaining, proto) = protocol(context, remaining)?;
let (remaining, _separator) = tag(":")(remaining)?; let (remaining, _separator) = tag(":")(remaining)?;
@@ -26,18 +30,21 @@ pub fn angle_link<'r, 's>(context: Context<'r, 's>, input: &'s str) -> Res<&'s s
Ok(( Ok((
remaining, remaining,
AngleLink { AngleLink {
source, source: source.into(),
link_type: proto, link_type: proto.into(),
path, path: path.into(),
}, },
)) ))
} }
#[cfg_attr(feature = "tracing", tracing::instrument(ret, level = "debug"))] #[cfg_attr(feature = "tracing", tracing::instrument(ret, level = "debug"))]
fn path_angle<'r, 's>(context: Context<'r, 's>, input: &'s str) -> Res<&'s str, &'s str> { fn path_angle<'r, 's>(
context: Context<'r, 's>,
input: OrgSource<'s>,
) -> Res<OrgSource<'s>, OrgSource<'s>> {
let parser_context = let parser_context =
context.with_additional_node(ContextElement::ExitMatcherNode(ExitMatcherNode { context.with_additional_node(ContextElement::ExitMatcherNode(ExitMatcherNode {
class: ExitClass::Beta, class: ExitClass::Gamma,
exit_matcher: &path_angle_end, exit_matcher: &path_angle_end,
})); }));
@@ -48,6 +55,9 @@ fn path_angle<'r, 's>(context: Context<'r, 's>, input: &'s str) -> Res<&'s str,
} }
#[cfg_attr(feature = "tracing", tracing::instrument(ret, level = "debug"))] #[cfg_attr(feature = "tracing", tracing::instrument(ret, level = "debug"))]
fn path_angle_end<'r, 's>(context: Context<'r, 's>, input: &'s str) -> Res<&'s str, &'s str> { fn path_angle_end<'r, 's>(
_context: Context<'r, 's>,
input: OrgSource<'s>,
) -> Res<OrgSource<'s>, OrgSource<'s>> {
tag(">")(input) tag(">")(input)
} }

View File

@@ -11,6 +11,7 @@ use nom::multi::many_till;
use nom::multi::separated_list1; use nom::multi::separated_list1;
use nom::sequence::tuple; use nom::sequence::tuple;
use super::org_source::OrgSource;
use super::Context; use super::Context;
use crate::error::CustomError; use crate::error::CustomError;
use crate::error::Res; use crate::error::Res;
@@ -29,7 +30,10 @@ use crate::parser::util::get_consumed;
use crate::parser::Object; use crate::parser::Object;
#[cfg_attr(feature = "tracing", tracing::instrument(ret, level = "debug"))] #[cfg_attr(feature = "tracing", tracing::instrument(ret, level = "debug"))]
pub fn citation<'r, 's>(context: Context<'r, 's>, input: &'s str) -> Res<&'s str, Citation<'s>> { pub fn citation<'r, 's>(
context: Context<'r, 's>,
input: OrgSource<'s>,
) -> Res<OrgSource<'s>, Citation<'s>> {
// TODO: Despite being a standard object, citations cannot exist inside the global prefix/suffix for other citations because citations must contain something that matches @key which is forbidden inside the global prefix/suffix. This TODO is to evaluate if its worth putting in an explicit check for this (which can be easily accomplished by checking the output of `get_bracket_depth()`). I suspect its not worth it because I expect, outside of intentionally crafted inputs, this parser will exit immediately inside a citation since it is unlikely to find the "[cite" substring inside a citation global prefix/suffix. // TODO: Despite being a standard object, citations cannot exist inside the global prefix/suffix for other citations because citations must contain something that matches @key which is forbidden inside the global prefix/suffix. This TODO is to evaluate if its worth putting in an explicit check for this (which can be easily accomplished by checking the output of `get_bracket_depth()`). I suspect its not worth it because I expect, outside of intentionally crafted inputs, this parser will exit immediately inside a citation since it is unlikely to find the "[cite" substring inside a citation global prefix/suffix.
let (remaining, _) = tag_no_case("[cite")(input)?; let (remaining, _) = tag_no_case("[cite")(input)?;
let (remaining, _) = opt(citestyle)(remaining)?; let (remaining, _) = opt(citestyle)(remaining)?;
@@ -44,11 +48,16 @@ pub fn citation<'r, 's>(context: Context<'r, 's>, input: &'s str) -> Res<&'s str
let (remaining, _) = tag("]")(remaining)?; let (remaining, _) = tag("]")(remaining)?;
let (remaining, _) = space0(remaining)?; let (remaining, _) = space0(remaining)?;
let source = get_consumed(input, remaining); let source = get_consumed(input, remaining);
Ok((remaining, Citation { source })) Ok((
remaining,
Citation {
source: source.into(),
},
))
} }
#[cfg_attr(feature = "tracing", tracing::instrument(ret, level = "debug"))] #[cfg_attr(feature = "tracing", tracing::instrument(ret, level = "debug"))]
fn citestyle<'r, 's>(input: &'s str) -> Res<&'s str, &'s str> { fn citestyle<'r, 's>(input: OrgSource<'s>) -> Res<OrgSource<'s>, OrgSource<'s>> {
let (remaining, _) = tuple((tag("/"), style))(input)?; let (remaining, _) = tuple((tag("/"), style))(input)?;
let (remaining, _) = opt(tuple((tag("/"), variant)))(remaining)?; let (remaining, _) = opt(tuple((tag("/"), variant)))(remaining)?;
let source = get_consumed(input, remaining); let source = get_consumed(input, remaining);
@@ -56,14 +65,14 @@ fn citestyle<'r, 's>(input: &'s str) -> Res<&'s str, &'s str> {
} }
#[cfg_attr(feature = "tracing", tracing::instrument(ret, level = "debug"))] #[cfg_attr(feature = "tracing", tracing::instrument(ret, level = "debug"))]
fn style<'r, 's>(input: &'s str) -> Res<&'s str, &'s str> { fn style<'r, 's>(input: OrgSource<'s>) -> Res<OrgSource<'s>, OrgSource<'s>> {
recognize(many1(verify(anychar, |c| { recognize(many1(verify(anychar, |c| {
c.is_alphanumeric() || "_-".contains(*c) c.is_alphanumeric() || "_-".contains(*c)
})))(input) })))(input)
} }
#[cfg_attr(feature = "tracing", tracing::instrument(ret, level = "debug"))] #[cfg_attr(feature = "tracing", tracing::instrument(ret, level = "debug"))]
fn variant<'r, 's>(input: &'s str) -> Res<&'s str, &'s str> { fn variant<'r, 's>(input: OrgSource<'s>) -> Res<OrgSource<'s>, OrgSource<'s>> {
recognize(many1(verify(anychar, |c| { recognize(many1(verify(anychar, |c| {
c.is_alphanumeric() || "_-/".contains(*c) c.is_alphanumeric() || "_-/".contains(*c)
})))(input) })))(input)
@@ -72,8 +81,8 @@ fn variant<'r, 's>(input: &'s str) -> Res<&'s str, &'s str> {
#[cfg_attr(feature = "tracing", tracing::instrument(ret, level = "debug"))] #[cfg_attr(feature = "tracing", tracing::instrument(ret, level = "debug"))]
fn global_prefix<'r, 's>( fn global_prefix<'r, 's>(
context: Context<'r, 's>, context: Context<'r, 's>,
input: &'s str, input: OrgSource<'s>,
) -> Res<&'s str, Vec<Object<'s>>> { ) -> Res<OrgSource<'s>, Vec<Object<'s>>> {
// TODO: I could insert CitationBracket entries in the context after each matched object to reduce the scanning done for counting brackets which should be more efficient. // TODO: I could insert CitationBracket entries in the context after each matched object to reduce the scanning done for counting brackets which should be more efficient.
let parser_context = context let parser_context = context
.with_additional_node(ContextElement::CitationBracket(CitationBracket { .with_additional_node(ContextElement::CitationBracket(CitationBracket {
@@ -81,7 +90,7 @@ fn global_prefix<'r, 's>(
depth: 0, depth: 0,
})) }))
.with_additional_node(ContextElement::ExitMatcherNode(ExitMatcherNode { .with_additional_node(ContextElement::ExitMatcherNode(ExitMatcherNode {
class: ExitClass::Beta, class: ExitClass::Gamma,
exit_matcher: &global_prefix_end, exit_matcher: &global_prefix_end,
})); }));
let (remaining, (children, _exit_contents)) = verify( let (remaining, (children, _exit_contents)) = verify(
@@ -96,12 +105,15 @@ fn global_prefix<'r, 's>(
} }
#[cfg_attr(feature = "tracing", tracing::instrument(ret, level = "debug"))] #[cfg_attr(feature = "tracing", tracing::instrument(ret, level = "debug"))]
fn global_prefix_end<'r, 's>(context: Context<'r, 's>, input: &'s str) -> Res<&'s str, &'s str> { fn global_prefix_end<'r, 's>(
context: Context<'r, 's>,
input: OrgSource<'s>,
) -> Res<OrgSource<'s>, OrgSource<'s>> {
let context_depth = get_bracket_depth(context) let context_depth = get_bracket_depth(context)
.expect("This function should only be called from inside a citation."); .expect("This function should only be called from inside a citation.");
let text_since_context_entry = get_consumed(context_depth.position, input); let text_since_context_entry = get_consumed(context_depth.position, input);
let mut current_depth = context_depth.depth; let mut current_depth = context_depth.depth;
for c in text_since_context_entry.chars() { for c in Into::<&str>::into(text_since_context_entry).chars() {
match c { match c {
'[' => { '[' => {
current_depth += 1; current_depth += 1;
@@ -116,7 +128,7 @@ fn global_prefix_end<'r, 's>(context: Context<'r, 's>, input: &'s str) -> Res<&'
} }
} }
if current_depth == 0 { if current_depth == 0 {
let close_bracket = tag::<&str, &str, CustomError<&str>>("]")(input); let close_bracket = tag::<&str, OrgSource<'_>, CustomError<OrgSource<'_>>>("]")(input);
if close_bracket.is_ok() { if close_bracket.is_ok() {
return close_bracket; return close_bracket;
} }
@@ -130,8 +142,8 @@ fn global_prefix_end<'r, 's>(context: Context<'r, 's>, input: &'s str) -> Res<&'
#[cfg_attr(feature = "tracing", tracing::instrument(ret, level = "debug"))] #[cfg_attr(feature = "tracing", tracing::instrument(ret, level = "debug"))]
fn global_suffix<'r, 's>( fn global_suffix<'r, 's>(
context: Context<'r, 's>, context: Context<'r, 's>,
input: &'s str, input: OrgSource<'s>,
) -> Res<&'s str, Vec<Object<'s>>> { ) -> Res<OrgSource<'s>, Vec<Object<'s>>> {
// TODO: I could insert CitationBracket entries in the context after each matched object to reduce the scanning done for counting brackets which should be more efficient. // TODO: I could insert CitationBracket entries in the context after each matched object to reduce the scanning done for counting brackets which should be more efficient.
let parser_context = context let parser_context = context
.with_additional_node(ContextElement::CitationBracket(CitationBracket { .with_additional_node(ContextElement::CitationBracket(CitationBracket {
@@ -139,7 +151,7 @@ fn global_suffix<'r, 's>(
depth: 0, depth: 0,
})) }))
.with_additional_node(ContextElement::ExitMatcherNode(ExitMatcherNode { .with_additional_node(ContextElement::ExitMatcherNode(ExitMatcherNode {
class: ExitClass::Beta, class: ExitClass::Gamma,
exit_matcher: &global_suffix_end, exit_matcher: &global_suffix_end,
})); }));
let (remaining, (children, _exit_contents)) = verify( let (remaining, (children, _exit_contents)) = verify(
@@ -153,12 +165,15 @@ fn global_suffix<'r, 's>(
} }
#[cfg_attr(feature = "tracing", tracing::instrument(ret, level = "debug"))] #[cfg_attr(feature = "tracing", tracing::instrument(ret, level = "debug"))]
fn global_suffix_end<'r, 's>(context: Context<'r, 's>, input: &'s str) -> Res<&'s str, &'s str> { fn global_suffix_end<'r, 's>(
context: Context<'r, 's>,
input: OrgSource<'s>,
) -> Res<OrgSource<'s>, OrgSource<'s>> {
let context_depth = get_bracket_depth(context) let context_depth = get_bracket_depth(context)
.expect("This function should only be called from inside a citation."); .expect("This function should only be called from inside a citation.");
let text_since_context_entry = get_consumed(context_depth.position, input); let text_since_context_entry = get_consumed(context_depth.position, input);
let mut current_depth = context_depth.depth; let mut current_depth = context_depth.depth;
for c in text_since_context_entry.chars() { for c in Into::<&str>::into(text_since_context_entry).chars() {
match c { match c {
'[' => { '[' => {
current_depth += 1; current_depth += 1;
@@ -173,7 +188,7 @@ fn global_suffix_end<'r, 's>(context: Context<'r, 's>, input: &'s str) -> Res<&'
} }
} }
if current_depth == 0 { if current_depth == 0 {
let close_bracket = tag::<&str, &str, CustomError<&str>>("]")(input); let close_bracket = tag::<&str, OrgSource<'_>, CustomError<OrgSource<'_>>>("]")(input);
if close_bracket.is_ok() { if close_bracket.is_ok() {
return close_bracket; return close_bracket;
} }
@@ -188,24 +203,21 @@ fn global_suffix_end<'r, 's>(context: Context<'r, 's>, input: &'s str) -> Res<&'
mod tests { mod tests {
use super::*; use super::*;
use crate::parser::element_parser::element; use crate::parser::element_parser::element;
use crate::parser::parser_context::ContextElement;
use crate::parser::parser_context::ContextTree; use crate::parser::parser_context::ContextTree;
use crate::parser::parser_with_context::parser_with_context; use crate::parser::parser_with_context::parser_with_context;
use crate::parser::source::Source; use crate::parser::source::Source;
#[test] #[test]
fn citation_simple() { fn citation_simple() {
let input = "[cite:@foo]"; let input = OrgSource::new("[cite:@foo]");
let initial_context: ContextTree<'_, '_> = ContextTree::new(); let initial_context: ContextTree<'_, '_> = ContextTree::new();
let document_context = let paragraph_matcher = parser_with_context!(element(true))(&initial_context);
initial_context.with_additional_node(ContextElement::DocumentRoot(input));
let paragraph_matcher = parser_with_context!(element(true))(&document_context);
let (remaining, first_paragraph) = paragraph_matcher(input).expect("Parse first paragraph"); let (remaining, first_paragraph) = paragraph_matcher(input).expect("Parse first paragraph");
let first_paragraph = match first_paragraph { let first_paragraph = match first_paragraph {
crate::parser::Element::Paragraph(paragraph) => paragraph, crate::parser::Element::Paragraph(paragraph) => paragraph,
_ => panic!("Should be a paragraph!"), _ => panic!("Should be a paragraph!"),
}; };
assert_eq!(remaining, ""); assert_eq!(Into::<&str>::into(remaining), "");
assert_eq!(first_paragraph.get_source(), "[cite:@foo]"); assert_eq!(first_paragraph.get_source(), "[cite:@foo]");
assert_eq!(first_paragraph.children.len(), 1); assert_eq!(first_paragraph.children.len(), 1);
assert_eq!( assert_eq!(

View File

@@ -10,6 +10,7 @@ use nom::multi::many_till;
use nom::sequence::preceded; use nom::sequence::preceded;
use nom::sequence::tuple; use nom::sequence::tuple;
use super::org_source::OrgSource;
use super::Context; use super::Context;
use crate::error::CustomError; use crate::error::CustomError;
use crate::error::Res; use crate::error::Res;
@@ -28,21 +29,26 @@ use crate::parser::Object;
#[cfg_attr(feature = "tracing", tracing::instrument(ret, level = "debug"))] #[cfg_attr(feature = "tracing", tracing::instrument(ret, level = "debug"))]
pub fn citation_reference<'r, 's>( pub fn citation_reference<'r, 's>(
context: Context<'r, 's>, context: Context<'r, 's>,
input: &'s str, input: OrgSource<'s>,
) -> Res<&'s str, CitationReference<'s>> { ) -> Res<OrgSource<'s>, CitationReference<'s>> {
let (remaining, _prefix) = opt(parser_with_context!(key_prefix)(context))(input)?; let (remaining, _prefix) = opt(parser_with_context!(key_prefix)(context))(input)?;
let (remaining, _key) = parser_with_context!(citation_reference_key)(context)(remaining)?; let (remaining, _key) = parser_with_context!(citation_reference_key)(context)(remaining)?;
let (remaining, _suffix) = opt(parser_with_context!(key_suffix)(context))(remaining)?; let (remaining, _suffix) = opt(parser_with_context!(key_suffix)(context))(remaining)?;
let source = get_consumed(input, remaining); let source = get_consumed(input, remaining);
Ok((remaining, CitationReference { source })) Ok((
remaining,
CitationReference {
source: source.into(),
},
))
} }
#[cfg_attr(feature = "tracing", tracing::instrument(ret, level = "debug"))] #[cfg_attr(feature = "tracing", tracing::instrument(ret, level = "debug"))]
pub fn citation_reference_key<'r, 's>( pub fn citation_reference_key<'r, 's>(
context: Context<'r, 's>, context: Context<'r, 's>,
input: &'s str, input: OrgSource<'s>,
) -> Res<&'s str, &'s str> { ) -> Res<OrgSource<'s>, OrgSource<'s>> {
let (remaining, source) = recognize(tuple(( let (remaining, source) = recognize(tuple((
tag("@"), tag("@"),
many1(verify( many1(verify(
@@ -59,7 +65,10 @@ pub fn citation_reference_key<'r, 's>(
} }
#[cfg_attr(feature = "tracing", tracing::instrument(ret, level = "debug"))] #[cfg_attr(feature = "tracing", tracing::instrument(ret, level = "debug"))]
fn key_prefix<'r, 's>(context: Context<'r, 's>, input: &'s str) -> Res<&'s str, Vec<Object<'s>>> { fn key_prefix<'r, 's>(
context: Context<'r, 's>,
input: OrgSource<'s>,
) -> Res<OrgSource<'s>, Vec<Object<'s>>> {
// TODO: I could insert CitationBracket entries in the context after each matched object to reduce the scanning done for counting brackets which should be more efficient. // TODO: I could insert CitationBracket entries in the context after each matched object to reduce the scanning done for counting brackets which should be more efficient.
let parser_context = context let parser_context = context
.with_additional_node(ContextElement::CitationBracket(CitationBracket { .with_additional_node(ContextElement::CitationBracket(CitationBracket {
@@ -67,7 +76,7 @@ fn key_prefix<'r, 's>(context: Context<'r, 's>, input: &'s str) -> Res<&'s str,
depth: 0, depth: 0,
})) }))
.with_additional_node(ContextElement::ExitMatcherNode(ExitMatcherNode { .with_additional_node(ContextElement::ExitMatcherNode(ExitMatcherNode {
class: ExitClass::Beta, class: ExitClass::Gamma,
exit_matcher: &key_prefix_end, exit_matcher: &key_prefix_end,
})); }));
let (remaining, (children, _exit_contents)) = verify( let (remaining, (children, _exit_contents)) = verify(
@@ -81,7 +90,10 @@ fn key_prefix<'r, 's>(context: Context<'r, 's>, input: &'s str) -> Res<&'s str,
} }
#[cfg_attr(feature = "tracing", tracing::instrument(ret, level = "debug"))] #[cfg_attr(feature = "tracing", tracing::instrument(ret, level = "debug"))]
fn key_suffix<'r, 's>(context: Context<'r, 's>, input: &'s str) -> Res<&'s str, Vec<Object<'s>>> { fn key_suffix<'r, 's>(
context: Context<'r, 's>,
input: OrgSource<'s>,
) -> Res<OrgSource<'s>, Vec<Object<'s>>> {
// TODO: I could insert CitationBracket entries in the context after each matched object to reduce the scanning done for counting brackets which should be more efficient. // TODO: I could insert CitationBracket entries in the context after each matched object to reduce the scanning done for counting brackets which should be more efficient.
let parser_context = context let parser_context = context
.with_additional_node(ContextElement::CitationBracket(CitationBracket { .with_additional_node(ContextElement::CitationBracket(CitationBracket {
@@ -89,7 +101,7 @@ fn key_suffix<'r, 's>(context: Context<'r, 's>, input: &'s str) -> Res<&'s str,
depth: 0, depth: 0,
})) }))
.with_additional_node(ContextElement::ExitMatcherNode(ExitMatcherNode { .with_additional_node(ContextElement::ExitMatcherNode(ExitMatcherNode {
class: ExitClass::Beta, class: ExitClass::Gamma,
exit_matcher: &key_suffix_end, exit_matcher: &key_suffix_end,
})); }));
let (remaining, (children, _exit_contents)) = verify( let (remaining, (children, _exit_contents)) = verify(
@@ -114,12 +126,15 @@ pub fn get_bracket_depth<'r, 's>(context: Context<'r, 's>) -> Option<&'r Citatio
} }
#[cfg_attr(feature = "tracing", tracing::instrument(ret, level = "debug"))] #[cfg_attr(feature = "tracing", tracing::instrument(ret, level = "debug"))]
fn key_prefix_end<'r, 's>(context: Context<'r, 's>, input: &'s str) -> Res<&'s str, &'s str> { fn key_prefix_end<'r, 's>(
context: Context<'r, 's>,
input: OrgSource<'s>,
) -> Res<OrgSource<'s>, OrgSource<'s>> {
let context_depth = get_bracket_depth(context) let context_depth = get_bracket_depth(context)
.expect("This function should only be called from inside a citation reference."); .expect("This function should only be called from inside a citation reference.");
let text_since_context_entry = get_consumed(context_depth.position, input); let text_since_context_entry = get_consumed(context_depth.position, input);
let mut current_depth = context_depth.depth; let mut current_depth = context_depth.depth;
for c in text_since_context_entry.chars() { for c in Into::<&str>::into(text_since_context_entry).chars() {
match c { match c {
'[' => { '[' => {
current_depth += 1; current_depth += 1;
@@ -134,7 +149,7 @@ fn key_prefix_end<'r, 's>(context: Context<'r, 's>, input: &'s str) -> Res<&'s s
} }
} }
if current_depth == 0 { if current_depth == 0 {
let close_bracket = tag::<&str, &str, CustomError<&str>>("]")(input); let close_bracket = tag::<&str, OrgSource<'_>, CustomError<OrgSource<'_>>>("]")(input);
if close_bracket.is_ok() { if close_bracket.is_ok() {
return close_bracket; return close_bracket;
} }
@@ -146,12 +161,15 @@ fn key_prefix_end<'r, 's>(context: Context<'r, 's>, input: &'s str) -> Res<&'s s
} }
#[cfg_attr(feature = "tracing", tracing::instrument(ret, level = "debug"))] #[cfg_attr(feature = "tracing", tracing::instrument(ret, level = "debug"))]
fn key_suffix_end<'r, 's>(context: Context<'r, 's>, input: &'s str) -> Res<&'s str, &'s str> { fn key_suffix_end<'r, 's>(
context: Context<'r, 's>,
input: OrgSource<'s>,
) -> Res<OrgSource<'s>, OrgSource<'s>> {
let context_depth = get_bracket_depth(context) let context_depth = get_bracket_depth(context)
.expect("This function should only be called from inside a citation reference."); .expect("This function should only be called from inside a citation reference.");
let text_since_context_entry = get_consumed(context_depth.position, input); let text_since_context_entry = get_consumed(context_depth.position, input);
let mut current_depth = context_depth.depth; let mut current_depth = context_depth.depth;
for c in text_since_context_entry.chars() { for c in Into::<&str>::into(text_since_context_entry).chars() {
match c { match c {
'[' => { '[' => {
current_depth += 1; current_depth += 1;
@@ -166,7 +184,7 @@ fn key_suffix_end<'r, 's>(context: Context<'r, 's>, input: &'s str) -> Res<&'s s
} }
} }
if current_depth == 0 { if current_depth == 0 {
let close_bracket = tag::<&str, &str, CustomError<&str>>("]")(input); let close_bracket = tag::<&str, OrgSource<'_>, CustomError<OrgSource<'_>>>("]")(input);
if close_bracket.is_ok() { if close_bracket.is_ok() {
return close_bracket; return close_bracket;
} }

View File

@@ -11,6 +11,7 @@ use nom::combinator::recognize;
use nom::combinator::verify; use nom::combinator::verify;
use nom::sequence::tuple; use nom::sequence::tuple;
use super::org_source::OrgSource;
use super::Context; use super::Context;
use crate::error::Res; use crate::error::Res;
use crate::parser::parser_with_context::parser_with_context; use crate::parser::parser_with_context::parser_with_context;
@@ -19,8 +20,11 @@ use crate::parser::util::start_of_line;
use crate::parser::Clock; use crate::parser::Clock;
#[cfg_attr(feature = "tracing", tracing::instrument(ret, level = "debug"))] #[cfg_attr(feature = "tracing", tracing::instrument(ret, level = "debug"))]
pub fn clock<'r, 's>(context: Context<'r, 's>, input: &'s str) -> Res<&'s str, Clock<'s>> { pub fn clock<'r, 's>(
start_of_line(context, input)?; context: Context<'r, 's>,
input: OrgSource<'s>,
) -> Res<OrgSource<'s>, Clock<'s>> {
start_of_line(input)?;
let (remaining, _leading_whitespace) = space0(input)?; let (remaining, _leading_whitespace) = space0(input)?;
let (remaining, _clock) = tag_no_case("clock:")(remaining)?; let (remaining, _clock) = tag_no_case("clock:")(remaining)?;
let (remaining, _gap_whitespace) = space1(remaining)?; let (remaining, _gap_whitespace) = space1(remaining)?;
@@ -31,14 +35,19 @@ pub fn clock<'r, 's>(context: Context<'r, 's>, input: &'s str) -> Res<&'s str, C
))(remaining)?; ))(remaining)?;
let source = get_consumed(input, remaining); let source = get_consumed(input, remaining);
Ok((remaining, Clock { source })) Ok((
remaining,
Clock {
source: source.into(),
},
))
} }
#[cfg_attr(feature = "tracing", tracing::instrument(ret, level = "debug"))] #[cfg_attr(feature = "tracing", tracing::instrument(ret, level = "debug"))]
fn inactive_timestamp_range_duration<'r, 's>( fn inactive_timestamp_range_duration<'r, 's>(
context: Context<'r, 's>, _context: Context<'r, 's>,
input: &'s str, input: OrgSource<'s>,
) -> Res<&'s str, &'s str> { ) -> Res<OrgSource<'s>, OrgSource<'s>> {
recognize(tuple(( recognize(tuple((
tag("["), tag("["),
is_not("\r\n]"), is_not("\r\n]"),
@@ -50,14 +59,17 @@ fn inactive_timestamp_range_duration<'r, 's>(
space1, space1,
digit1, digit1,
tag(":"), tag(":"),
verify(digit1, |mm: &str| mm.len() == 2), verify(digit1, |mm: &OrgSource<'_>| mm.len() == 2),
space0, space0,
alt((line_ending, eof)), alt((line_ending, eof)),
)))(input) )))(input)
} }
#[cfg_attr(feature = "tracing", tracing::instrument(ret, level = "debug"))] #[cfg_attr(feature = "tracing", tracing::instrument(ret, level = "debug"))]
fn inactive_timestamp<'r, 's>(context: Context<'r, 's>, input: &'s str) -> Res<&'s str, &'s str> { fn inactive_timestamp<'r, 's>(
_context: Context<'r, 's>,
input: OrgSource<'s>,
) -> Res<OrgSource<'s>, OrgSource<'s>> {
recognize(tuple(( recognize(tuple((
tag("["), tag("["),
is_not("\r\n]"), is_not("\r\n]"),

View File

@@ -11,6 +11,7 @@ use nom::multi::many0;
use nom::sequence::preceded; use nom::sequence::preceded;
use nom::sequence::tuple; use nom::sequence::tuple;
use super::org_source::OrgSource;
use super::util::get_consumed; use super::util::get_consumed;
use super::Context; use super::Context;
use crate::error::CustomError; use crate::error::CustomError;
@@ -24,10 +25,13 @@ use crate::parser::util::start_of_line;
use crate::parser::Comment; use crate::parser::Comment;
#[cfg_attr(feature = "tracing", tracing::instrument(ret, level = "debug"))] #[cfg_attr(feature = "tracing", tracing::instrument(ret, level = "debug"))]
pub fn comment<'r, 's>(context: Context<'r, 's>, input: &'s str) -> Res<&'s str, Comment<'s>> { pub fn comment<'r, 's>(
context: Context<'r, 's>,
input: OrgSource<'s>,
) -> Res<OrgSource<'s>, Comment<'s>> {
if immediate_in_section(context, "comment") { if immediate_in_section(context, "comment") {
return Err(nom::Err::Error(CustomError::MyError(MyError( return Err(nom::Err::Error(CustomError::MyError(MyError(
"Cannot nest objects of the same element", "Cannot nest objects of the same element".into(),
)))); ))));
} }
let parser_context = context.with_additional_node(ContextElement::Context("comment")); let parser_context = context.with_additional_node(ContextElement::Context("comment"));
@@ -38,12 +42,20 @@ pub fn comment<'r, 's>(context: Context<'r, 's>, input: &'s str) -> Res<&'s str,
many0(preceded(not(exit_matcher), comment_line_matcher))(remaining)?; many0(preceded(not(exit_matcher), comment_line_matcher))(remaining)?;
let source = get_consumed(input, remaining); let source = get_consumed(input, remaining);
Ok((remaining, Comment { source })) Ok((
remaining,
Comment {
source: source.into(),
},
))
} }
#[cfg_attr(feature = "tracing", tracing::instrument(ret, level = "debug"))] #[cfg_attr(feature = "tracing", tracing::instrument(ret, level = "debug"))]
fn comment_line<'r, 's>(context: Context<'r, 's>, input: &'s str) -> Res<&'s str, &'s str> { fn comment_line<'r, 's>(
start_of_line(context, input)?; _context: Context<'r, 's>,
input: OrgSource<'s>,
) -> Res<OrgSource<'s>, OrgSource<'s>> {
start_of_line(input)?;
let (remaining, _indent) = space0(input)?; let (remaining, _indent) = space0(input)?;
let (remaining, (_hash, _leading_whitespace_and_content, _line_ending)) = tuple(( let (remaining, (_hash, _leading_whitespace_and_content, _line_ending)) = tuple((
tag("#"), tag("#"),
@@ -57,22 +69,21 @@ fn comment_line<'r, 's>(context: Context<'r, 's>, input: &'s str) -> Res<&'s str
#[cfg(test)] #[cfg(test)]
mod tests { mod tests {
use super::*; use super::*;
use crate::parser::parser_context::ContextElement;
use crate::parser::parser_context::ContextTree; use crate::parser::parser_context::ContextTree;
use crate::parser::parser_with_context::parser_with_context; use crate::parser::parser_with_context::parser_with_context;
#[test] #[test]
fn require_space_after_hash() { fn require_space_after_hash() {
let input = "# Comment line let input = OrgSource::new(
"# Comment line
#not a comment #not a comment
# Comment again"; # Comment again",
);
let initial_context: ContextTree<'_, '_> = ContextTree::new(); let initial_context: ContextTree<'_, '_> = ContextTree::new();
let document_context = let comment_matcher = parser_with_context!(comment)(&initial_context);
initial_context.with_additional_node(ContextElement::DocumentRoot(input));
let comment_matcher = parser_with_context!(comment)(&document_context);
let (remaining, first_comment) = comment_matcher(input).expect("Parse first comment"); let (remaining, first_comment) = comment_matcher(input).expect("Parse first comment");
assert_eq!( assert_eq!(
remaining, Into::<&str>::into(remaining),
r#"#not a comment r#"#not a comment
# Comment again"# # Comment again"#
); );

View File

@@ -6,6 +6,7 @@ use nom::combinator::eof;
use nom::combinator::recognize; use nom::combinator::recognize;
use nom::sequence::tuple; use nom::sequence::tuple;
use super::org_source::OrgSource;
use super::sexp::sexp; use super::sexp::sexp;
use super::Context; use super::Context;
use crate::error::Res; use crate::error::Res;
@@ -14,8 +15,11 @@ use crate::parser::util::start_of_line;
use crate::parser::DiarySexp; use crate::parser::DiarySexp;
#[cfg_attr(feature = "tracing", tracing::instrument(ret, level = "debug"))] #[cfg_attr(feature = "tracing", tracing::instrument(ret, level = "debug"))]
pub fn diary_sexp<'r, 's>(context: Context<'r, 's>, input: &'s str) -> Res<&'s str, DiarySexp<'s>> { pub fn diary_sexp<'r, 's>(
start_of_line(context, input)?; _context: Context<'r, 's>,
input: OrgSource<'s>,
) -> Res<OrgSource<'s>, DiarySexp<'s>> {
start_of_line(input)?;
let (remaining, _leading_whitespace) = space0(input)?; let (remaining, _leading_whitespace) = space0(input)?;
let (remaining, _clock) = tag("%%")(remaining)?; let (remaining, _clock) = tag("%%")(remaining)?;
let (remaining, _gap_whitespace) = space0(remaining)?; let (remaining, _gap_whitespace) = space0(remaining)?;
@@ -24,5 +28,10 @@ pub fn diary_sexp<'r, 's>(context: Context<'r, 's>, input: &'s str) -> Res<&'s s
recognize(tuple((space0, alt((line_ending, eof)))))(remaining)?; recognize(tuple((space0, alt((line_ending, eof)))))(remaining)?;
let source = get_consumed(input, remaining); let source = get_consumed(input, remaining);
Ok((remaining, DiarySexp { source })) Ok((
remaining,
DiarySexp {
source: source.into(),
},
))
} }

View File

@@ -16,6 +16,8 @@ use nom::sequence::tuple;
use super::element::Element; use super::element::Element;
use super::object::Object; use super::object::Object;
use super::org_source::convert_error;
use super::org_source::OrgSource;
use super::parser_with_context::parser_with_context; use super::parser_with_context::parser_with_context;
use super::source::Source; use super::source::Source;
use super::token::AllTokensIterator; use super::token::AllTokensIterator;
@@ -95,9 +97,10 @@ impl<'s> Source<'s> for Heading<'s> {
#[allow(dead_code)] #[allow(dead_code)]
pub fn document(input: &str) -> Res<&str, Document> { pub fn document(input: &str) -> Res<&str, Document> {
let initial_context: ContextTree<'_, '_> = ContextTree::new(); let initial_context: ContextTree<'_, '_> = ContextTree::new();
let document_context = let wrapped_input = OrgSource::new(input);
initial_context.with_additional_node(ContextElement::DocumentRoot(input)); let (remaining, document) = _document(&initial_context, wrapped_input)
let (remaining, document) = _document(&document_context, input)?; .map(|(rem, out)| (Into::<&str>::into(rem), out))
.map_err(convert_error)?;
{ {
// If there are radio targets in this document then we need to parse the entire document again with the knowledge of the radio targets. // If there are radio targets in this document then we need to parse the entire document again with the knowledge of the radio targets.
let all_radio_targets: Vec<&Vec<Object<'_>>> = document let all_radio_targets: Vec<&Vec<Object<'_>>> = document
@@ -113,17 +116,22 @@ pub fn document(input: &str) -> Res<&str, Document> {
.map(|rt| &rt.children) .map(|rt| &rt.children)
.collect(); .collect();
if !all_radio_targets.is_empty() { if !all_radio_targets.is_empty() {
let document_context = document_context let initial_context = initial_context
.with_additional_node(ContextElement::RadioTarget(all_radio_targets)); .with_additional_node(ContextElement::RadioTarget(all_radio_targets));
let (remaining, document) = _document(&document_context, input)?; let (remaining, document) = _document(&initial_context, wrapped_input)
return Ok((remaining, document)); .map(|(rem, out)| (Into::<&str>::into(rem), out))
.map_err(convert_error)?;
return Ok((remaining.into(), document));
} }
} }
Ok((remaining, document)) Ok((remaining.into(), document))
} }
#[cfg_attr(feature = "tracing", tracing::instrument(ret, level = "debug"))] #[cfg_attr(feature = "tracing", tracing::instrument(ret, level = "debug"))]
pub fn _document<'r, 's>(context: Context<'r, 's>, input: &'s str) -> Res<&'s str, Document<'s>> { fn _document<'r, 's>(
context: Context<'r, 's>,
input: OrgSource<'s>,
) -> Res<OrgSource<'s>, Document<'s>> {
let zeroth_section_matcher = parser_with_context!(zeroth_section)(context); let zeroth_section_matcher = parser_with_context!(zeroth_section)(context);
let heading_matcher = parser_with_context!(heading)(context); let heading_matcher = parser_with_context!(heading)(context);
let (remaining, _blank_lines) = many0(blank_line)(input)?; let (remaining, _blank_lines) = many0(blank_line)(input)?;
@@ -133,7 +141,7 @@ pub fn _document<'r, 's>(context: Context<'r, 's>, input: &'s str) -> Res<&'s st
Ok(( Ok((
remaining, remaining,
Document { Document {
source, source: source.into(),
zeroth_section, zeroth_section,
children, children,
}, },
@@ -141,7 +149,10 @@ pub fn _document<'r, 's>(context: Context<'r, 's>, input: &'s str) -> Res<&'s st
} }
#[cfg_attr(feature = "tracing", tracing::instrument(ret, level = "debug"))] #[cfg_attr(feature = "tracing", tracing::instrument(ret, level = "debug"))]
fn zeroth_section<'r, 's>(context: Context<'r, 's>, input: &'s str) -> Res<&'s str, Section<'s>> { fn zeroth_section<'r, 's>(
context: Context<'r, 's>,
input: OrgSource<'s>,
) -> Res<OrgSource<'s>, Section<'s>> {
// TODO: The zeroth section is specialized so it probably needs its own parser // TODO: The zeroth section is specialized so it probably needs its own parser
let parser_context = context let parser_context = context
.with_additional_node(ContextElement::ConsumeTrailingWhitespace(true)) .with_additional_node(ContextElement::ConsumeTrailingWhitespace(true))
@@ -160,7 +171,7 @@ fn zeroth_section<'r, 's>(context: Context<'r, 's>, input: &'s str) -> Res<&'s s
opt(parser_with_context!(comment)( opt(parser_with_context!(comment)(
&without_consuming_whitespace_context, &without_consuming_whitespace_context,
)), )),
parser_with_context!(property_drawer)(&without_consuming_whitespace_context), parser_with_context!(property_drawer)(context),
many0(blank_line), many0(blank_line),
)))(input)?; )))(input)?;
@@ -182,11 +193,20 @@ fn zeroth_section<'r, 's>(context: Context<'r, 's>, input: &'s str) -> Res<&'s s
maybe_consume_trailing_whitespace_if_not_exiting(context, remaining)?; maybe_consume_trailing_whitespace_if_not_exiting(context, remaining)?;
let source = get_consumed(input, remaining); let source = get_consumed(input, remaining);
Ok((remaining, Section { source, children })) Ok((
remaining,
Section {
source: source.into(),
children,
},
))
} }
#[cfg_attr(feature = "tracing", tracing::instrument(ret, level = "debug"))] #[cfg_attr(feature = "tracing", tracing::instrument(ret, level = "debug"))]
fn section<'r, 's>(context: Context<'r, 's>, mut input: &'s str) -> Res<&'s str, Section<'s>> { fn section<'r, 's>(
context: Context<'r, 's>,
mut input: OrgSource<'s>,
) -> Res<OrgSource<'s>, Section<'s>> {
// TODO: The zeroth section is specialized so it probably needs its own parser // TODO: The zeroth section is specialized so it probably needs its own parser
let parser_context = context let parser_context = context
.with_additional_node(ContextElement::ConsumeTrailingWhitespace(true)) .with_additional_node(ContextElement::ConsumeTrailingWhitespace(true))
@@ -223,17 +243,29 @@ fn section<'r, 's>(context: Context<'r, 's>, mut input: &'s str) -> Res<&'s str,
maybe_consume_trailing_whitespace_if_not_exiting(context, remaining)?; maybe_consume_trailing_whitespace_if_not_exiting(context, remaining)?;
let source = get_consumed(input, remaining); let source = get_consumed(input, remaining);
Ok((remaining, Section { source, children })) Ok((
remaining,
Section {
source: source.into(),
children,
},
))
} }
#[cfg_attr(feature = "tracing", tracing::instrument(ret, level = "debug"))] #[cfg_attr(feature = "tracing", tracing::instrument(ret, level = "debug"))]
fn section_end<'r, 's>(context: Context<'r, 's>, input: &'s str) -> Res<&'s str, &'s str> { fn section_end<'r, 's>(
context: Context<'r, 's>,
input: OrgSource<'s>,
) -> Res<OrgSource<'s>, OrgSource<'s>> {
let headline_matcher = parser_with_context!(headline)(context); let headline_matcher = parser_with_context!(headline)(context);
recognize(headline_matcher)(input) recognize(headline_matcher)(input)
} }
#[cfg_attr(feature = "tracing", tracing::instrument(ret, level = "debug"))] #[cfg_attr(feature = "tracing", tracing::instrument(ret, level = "debug"))]
fn heading<'r, 's>(context: Context<'r, 's>, input: &'s str) -> Res<&'s str, Heading<'s>> { fn heading<'r, 's>(
context: Context<'r, 's>,
input: OrgSource<'s>,
) -> Res<OrgSource<'s>, Heading<'s>> {
not(|i| context.check_exit_matcher(i))(input)?; not(|i| context.check_exit_matcher(i))(input)?;
let (remaining, (star_count, _ws, title)) = headline(context, input)?; let (remaining, (star_count, _ws, title)) = headline(context, input)?;
let section_matcher = parser_with_context!(section)(context); let section_matcher = parser_with_context!(section)(context);
@@ -249,7 +281,7 @@ fn heading<'r, 's>(context: Context<'r, 's>, input: &'s str) -> Res<&'s str, Hea
Ok(( Ok((
remaining, remaining,
Heading { Heading {
source, source: source.into(),
stars: star_count, stars: star_count,
title, title,
children, children,
@@ -260,18 +292,17 @@ fn heading<'r, 's>(context: Context<'r, 's>, input: &'s str) -> Res<&'s str, Hea
#[cfg_attr(feature = "tracing", tracing::instrument(ret, level = "debug"))] #[cfg_attr(feature = "tracing", tracing::instrument(ret, level = "debug"))]
fn headline<'r, 's>( fn headline<'r, 's>(
context: Context<'r, 's>, context: Context<'r, 's>,
input: &'s str, input: OrgSource<'s>,
) -> Res<&'s str, (usize, &'s str, Vec<Object<'s>>)> { ) -> Res<OrgSource<'s>, (usize, OrgSource<'s>, Vec<Object<'s>>)> {
let parser_context = let parser_context =
context.with_additional_node(ContextElement::ExitMatcherNode(ExitMatcherNode { context.with_additional_node(ContextElement::ExitMatcherNode(ExitMatcherNode {
class: ExitClass::Document, class: ExitClass::Document,
exit_matcher: &headline_end, exit_matcher: &headline_end,
})); }));
let standard_set_object_matcher = parser_with_context!(standard_set_object)(&parser_context); let standard_set_object_matcher = parser_with_context!(standard_set_object)(&parser_context);
let start_of_line_matcher = parser_with_context!(start_of_line)(&parser_context);
let (remaining, (_sol, star_count, ws, title, _line_ending)) = tuple(( let (remaining, (_sol, star_count, ws, title, _line_ending)) = tuple((
start_of_line_matcher, start_of_line,
many1_count(tag("*")), many1_count(tag("*")),
space1, space1,
many1(standard_set_object_matcher), many1(standard_set_object_matcher),
@@ -281,7 +312,10 @@ fn headline<'r, 's>(
} }
#[cfg_attr(feature = "tracing", tracing::instrument(ret, level = "debug"))] #[cfg_attr(feature = "tracing", tracing::instrument(ret, level = "debug"))]
fn headline_end<'r, 's>(_context: Context<'r, 's>, input: &'s str) -> Res<&'s str, &'s str> { fn headline_end<'r, 's>(
_context: Context<'r, 's>,
input: OrgSource<'s>,
) -> Res<OrgSource<'s>, OrgSource<'s>> {
line_ending(input) line_ending(input)
} }

View File

@@ -10,6 +10,7 @@ use nom::combinator::recognize;
use nom::multi::many_till; use nom::multi::many_till;
use nom::sequence::tuple; use nom::sequence::tuple;
use super::org_source::OrgSource;
use super::Context; use super::Context;
use crate::error::CustomError; use crate::error::CustomError;
use crate::error::MyError; use crate::error::MyError;
@@ -31,13 +32,16 @@ use crate::parser::Element;
use crate::parser::Paragraph; use crate::parser::Paragraph;
#[cfg_attr(feature = "tracing", tracing::instrument(ret, level = "debug"))] #[cfg_attr(feature = "tracing", tracing::instrument(ret, level = "debug"))]
pub fn drawer<'r, 's>(context: Context<'r, 's>, input: &'s str) -> Res<&'s str, Drawer<'s>> { pub fn drawer<'r, 's>(
context: Context<'r, 's>,
input: OrgSource<'s>,
) -> Res<OrgSource<'s>, Drawer<'s>> {
if immediate_in_section(context, "drawer") { if immediate_in_section(context, "drawer") {
return Err(nom::Err::Error(CustomError::MyError(MyError( return Err(nom::Err::Error(CustomError::MyError(MyError(
"Cannot nest objects of the same element", "Cannot nest objects of the same element".into(),
)))); ))));
} }
start_of_line(context, input)?; start_of_line(input)?;
let (remaining, _leading_whitespace) = space0(input)?; let (remaining, _leading_whitespace) = space0(input)?;
let (remaining, (_open_colon, drawer_name, _close_colon, _new_line)) = tuple(( let (remaining, (_open_colon, drawer_name, _close_colon, _new_line)) = tuple((
tag(":"), tag(":"),
@@ -63,9 +67,9 @@ pub fn drawer<'r, 's>(context: Context<'r, 's>, input: &'s str) -> Res<&'s str,
))(remaining) ))(remaining)
{ {
Ok((remain, (_not_immediate_exit, first_line, (_trailing_whitespace, _exit_contents)))) => { Ok((remain, (_not_immediate_exit, first_line, (_trailing_whitespace, _exit_contents)))) => {
let mut element = Element::Paragraph(Paragraph::of_text(first_line)); let mut element = Element::Paragraph(Paragraph::of_text(first_line.into()));
let source = get_consumed(remaining, remain); let source = get_consumed(remaining, remain);
element.set_source(source); element.set_source(source.into());
(remain, vec![element]) (remain, vec![element])
} }
Err(_) => { Err(_) => {
@@ -81,21 +85,24 @@ pub fn drawer<'r, 's>(context: Context<'r, 's>, input: &'s str) -> Res<&'s str,
Ok(( Ok((
remaining, remaining,
Drawer { Drawer {
source, source: source.into(),
name: drawer_name, name: drawer_name.into(),
children, children,
}, },
)) ))
} }
#[cfg_attr(feature = "tracing", tracing::instrument(ret, level = "debug"))] #[cfg_attr(feature = "tracing", tracing::instrument(ret, level = "debug"))]
fn name<'s>(input: &'s str) -> Res<&'s str, &'s str> { fn name<'s>(input: OrgSource<'s>) -> Res<OrgSource<'s>, OrgSource<'s>> {
take_while(|c| WORD_CONSTITUENT_CHARACTERS.contains(c) || "-_".contains(c))(input) take_while(|c| WORD_CONSTITUENT_CHARACTERS.contains(c) || "-_".contains(c))(input)
} }
#[cfg_attr(feature = "tracing", tracing::instrument(ret, level = "debug"))] #[cfg_attr(feature = "tracing", tracing::instrument(ret, level = "debug"))]
fn drawer_end<'r, 's>(context: Context<'r, 's>, input: &'s str) -> Res<&'s str, &'s str> { fn drawer_end<'r, 's>(
start_of_line(context, input)?; _context: Context<'r, 's>,
input: OrgSource<'s>,
) -> Res<OrgSource<'s>, OrgSource<'s>> {
start_of_line(input)?;
recognize(tuple(( recognize(tuple((
space0, space0,
tag_no_case(":end:"), tag_no_case(":end:"),

View File

@@ -11,6 +11,7 @@ use nom::combinator::recognize;
use nom::multi::many_till; use nom::multi::many_till;
use nom::sequence::tuple; use nom::sequence::tuple;
use super::org_source::OrgSource;
use super::Context; use super::Context;
use crate::error::CustomError; use crate::error::CustomError;
use crate::error::MyError; use crate::error::MyError;
@@ -33,15 +34,15 @@ use crate::parser::Element;
#[cfg_attr(feature = "tracing", tracing::instrument(ret, level = "debug"))] #[cfg_attr(feature = "tracing", tracing::instrument(ret, level = "debug"))]
pub fn dynamic_block<'r, 's>( pub fn dynamic_block<'r, 's>(
context: Context<'r, 's>, context: Context<'r, 's>,
input: &'s str, input: OrgSource<'s>,
) -> Res<&'s str, DynamicBlock<'s>> { ) -> Res<OrgSource<'s>, DynamicBlock<'s>> {
// TODO: Do I need to differentiate between different dynamic block types. // TODO: Do I need to differentiate between different dynamic block types.
if immediate_in_section(context, "dynamic block") { if immediate_in_section(context, "dynamic block") {
return Err(nom::Err::Error(CustomError::MyError(MyError( return Err(nom::Err::Error(CustomError::MyError(MyError(
"Cannot nest objects of the same element", "Cannot nest objects of the same element".into(),
)))); ))));
} }
start_of_line(context, input)?; start_of_line(input)?;
let (remaining, _leading_whitespace) = space0(input)?; let (remaining, _leading_whitespace) = space0(input)?;
let (remaining, (_begin, name, parameters, _ws)) = tuple(( let (remaining, (_begin, name, parameters, _ws)) = tuple((
recognize(tuple((tag_no_case("#+begin:"), space1))), recognize(tuple((tag_no_case("#+begin:"), space1))),
@@ -69,9 +70,9 @@ pub fn dynamic_block<'r, 's>(
))(remaining) ))(remaining)
{ {
Ok((remain, (_not_immediate_exit, first_line, (_trailing_whitespace, _exit_contents)))) => { Ok((remain, (_not_immediate_exit, first_line, (_trailing_whitespace, _exit_contents)))) => {
let mut element = Element::Paragraph(Paragraph::of_text(first_line)); let mut element = Element::Paragraph(Paragraph::of_text(first_line.into()));
let source = get_consumed(remaining, remain); let source = get_consumed(remaining, remain);
element.set_source(source); element.set_source(source.into());
(remain, vec![element]) (remain, vec![element])
} }
Err(_) => { Err(_) => {
@@ -86,27 +87,30 @@ pub fn dynamic_block<'r, 's>(
Ok(( Ok((
remaining, remaining,
DynamicBlock { DynamicBlock {
source, source: source.into(),
name, name: name.into(),
parameters, parameters: parameters.map(|val| val.into()),
children, children,
}, },
)) ))
} }
#[cfg_attr(feature = "tracing", tracing::instrument(ret, level = "debug"))] #[cfg_attr(feature = "tracing", tracing::instrument(ret, level = "debug"))]
fn name<'s>(input: &'s str) -> Res<&'s str, &'s str> { fn name<'s>(input: OrgSource<'s>) -> Res<OrgSource<'s>, OrgSource<'s>> {
is_not(" \t\r\n")(input) is_not(" \t\r\n")(input)
} }
#[cfg_attr(feature = "tracing", tracing::instrument(ret, level = "debug"))] #[cfg_attr(feature = "tracing", tracing::instrument(ret, level = "debug"))]
fn parameters<'s>(input: &'s str) -> Res<&'s str, &'s str> { fn parameters<'s>(input: OrgSource<'s>) -> Res<OrgSource<'s>, OrgSource<'s>> {
is_not("\r\n")(input) is_not("\r\n")(input)
} }
#[cfg_attr(feature = "tracing", tracing::instrument(ret, level = "debug"))] #[cfg_attr(feature = "tracing", tracing::instrument(ret, level = "debug"))]
fn dynamic_block_end<'r, 's>(context: Context<'r, 's>, input: &'s str) -> Res<&'s str, &'s str> { fn dynamic_block_end<'r, 's>(
start_of_line(context, input)?; _context: Context<'r, 's>,
input: OrgSource<'s>,
) -> Res<OrgSource<'s>, OrgSource<'s>> {
start_of_line(input)?;
let (remaining, source) = recognize(tuple(( let (remaining, source) = recognize(tuple((
space0, space0,
tag_no_case("#+end:"), tag_no_case("#+end:"),

Some files were not shown because too many files have changed in this diff Show More