Compare commits
121 Commits
remove_tek
...
v0.1.6
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
612744ebd0 | ||
|
|
1b4b8b4bdb | ||
|
|
5587e19f16 | ||
|
|
80f7098f9b | ||
|
|
84d2babda9 | ||
|
|
cc56b79683 | ||
|
|
0105b49d0d | ||
|
|
d79035e14d | ||
|
|
7545fb7e1a | ||
|
|
f30069efe7 | ||
|
|
d1fe2f6b09 | ||
|
|
21c60d1036 | ||
|
|
6a1bdd5fee | ||
|
|
5d20d3e99b | ||
|
|
a8fbf01124 | ||
|
|
344ef04453 | ||
|
|
ceb722e476 | ||
|
|
b04341882c | ||
|
|
494fe5cceb | ||
|
|
0110d23387 | ||
|
|
0d7a15bfeb | ||
|
|
352c20d1d8 | ||
|
|
f82d2aada1 | ||
|
|
669da4073e | ||
|
|
0056657b65 | ||
|
|
8780976c15 | ||
|
|
dc8b8d08ab | ||
|
|
93d3d9471f | ||
|
|
c7c0deed74 | ||
|
|
b32c21eb1d | ||
|
|
2e6e6fdd2b | ||
|
|
3cc2294387 | ||
|
|
40f22034da | ||
|
|
ab612f293f | ||
|
|
57c2922e4a | ||
|
|
c2eb1f51c8 | ||
|
|
b0930df788 | ||
|
|
69512f559a | ||
|
|
76a81b73ac | ||
|
|
ba291c6776 | ||
|
|
6b82b46e09 | ||
|
|
6676012eb1 | ||
|
|
facbe716e9 | ||
|
|
827f3e1c98 | ||
|
|
fcea7e5a4b | ||
|
|
dda2b1e69f | ||
|
|
f79d07a7c8 | ||
|
|
45283b48d9 | ||
|
|
08e4c646e5 | ||
|
|
f8b99ed235 | ||
|
|
6fc607cfe0 | ||
|
|
49afcf0db6 | ||
|
|
c4d7e646fc | ||
|
|
3fc3a5d1ef | ||
|
|
6e2fc362ea | ||
|
|
90fa48661c | ||
|
|
5cefcd5fac | ||
|
|
b83a103c17 | ||
|
|
d90ff5891b | ||
|
|
a3c01805b8 | ||
|
|
e3d755317d | ||
|
|
b89607fc8b | ||
|
|
51c4e2b62a | ||
|
|
a6561d37fb | ||
|
|
4e8b3eb422 | ||
|
|
2c31590974 | ||
|
|
28b2d27054 | ||
|
|
84edd10864 | ||
|
|
728a79f9a4 | ||
|
|
ad4ef50669 | ||
|
|
12cbb89861 | ||
|
|
7c471ab32e | ||
|
|
400f53e440 | ||
|
|
028aeb70aa | ||
|
|
70fafd801e | ||
|
|
bdba495f69 | ||
|
|
b0392ad6fb | ||
|
|
1c142b68c6 | ||
|
|
9060f9b26d | ||
|
|
d3c733c5ad | ||
|
|
275b4b53d1 | ||
|
|
d38e198258 | ||
|
|
27cf6c0462 | ||
|
|
c7d5c89a60 | ||
|
|
ee02e07717 | ||
|
|
a7330e38e4 | ||
|
|
08eb59acd3 | ||
|
|
da1ce2717d | ||
|
|
a8f277efe5 | ||
|
|
7f6f22717b | ||
|
|
0ef141d65e | ||
|
|
71180d19fb | ||
|
|
33091112a5 | ||
|
|
5997567233 | ||
|
|
2915a81edc | ||
|
|
df79cbd0b7 | ||
|
|
a7b9eb9db4 | ||
|
|
d262833f9b | ||
|
|
0d438a8e0f | ||
|
|
0b009511ff | ||
|
|
3bdb24ad88 | ||
|
|
fdf35ba23c | ||
|
|
cd69e08516 | ||
|
|
b54c6d366c | ||
|
|
15e8d1ab77 | ||
|
|
8502a8830d | ||
|
|
74a6101de7 | ||
|
|
ba57eb16fd | ||
|
|
c309d14776 | ||
|
|
0d728510d7 | ||
|
|
22e9bc991f | ||
|
|
564104f1e8 | ||
|
|
12ad3b09f0 | ||
|
|
eabffe5ecc | ||
|
|
b47029fdbb | ||
|
|
25b8c80d4e | ||
|
|
54825538e4 | ||
|
|
66d10a7a1b | ||
|
|
acf1205e75 | ||
|
|
2cd2f7570c | ||
|
|
f16a554154 |
203
.lighthouse/pipeline-foreign-document-test.yaml
Normal file
203
.lighthouse/pipeline-foreign-document-test.yaml
Normal file
@@ -0,0 +1,203 @@
|
||||
apiVersion: tekton.dev/v1beta1
|
||||
kind: PipelineRun
|
||||
metadata:
|
||||
name: rust-foreign-document-test
|
||||
spec:
|
||||
pipelineSpec:
|
||||
timeouts:
|
||||
pipeline: "2h0m0s"
|
||||
tasks: "1h0m40s"
|
||||
finally: "0h30m0s"
|
||||
params:
|
||||
- name: image-name
|
||||
description: The name for the built image
|
||||
type: string
|
||||
- name: path-to-image-context
|
||||
description: The path to the build context
|
||||
type: string
|
||||
- name: path-to-dockerfile
|
||||
description: The path to the Dockerfile
|
||||
type: string
|
||||
tasks:
|
||||
- name: do-stuff
|
||||
taskSpec:
|
||||
metadata: {}
|
||||
stepTemplate:
|
||||
image: alpine:3.18
|
||||
name: ""
|
||||
resources:
|
||||
requests:
|
||||
cpu: 10m
|
||||
memory: 600Mi
|
||||
workingDir: /workspace/source
|
||||
steps:
|
||||
- image: alpine:3.18
|
||||
name: do-stuff-step
|
||||
script: |
|
||||
#!/usr/bin/env sh
|
||||
echo "hello world"
|
||||
- name: report-pending
|
||||
taskRef:
|
||||
name: gitea-set-status
|
||||
runAfter:
|
||||
- fetch-repository
|
||||
params:
|
||||
- name: CONTEXT
|
||||
value: "$(params.JOB_NAME)"
|
||||
- name: REPO_FULL_NAME
|
||||
value: "$(params.REPO_OWNER)/$(params.REPO_NAME)"
|
||||
- name: GITEA_HOST_URL
|
||||
value: code.fizz.buzz
|
||||
- name: SHA
|
||||
value: "$(tasks.fetch-repository.results.commit)"
|
||||
- name: DESCRIPTION
|
||||
value: "Build $(params.JOB_NAME) has started"
|
||||
- name: STATE
|
||||
value: pending
|
||||
- name: TARGET_URL
|
||||
value: "https://tekton.fizz.buzz/#/namespaces/$(context.pipelineRun.namespace)/pipelineruns/$(context.pipelineRun.name)"
|
||||
- name: fetch-repository
|
||||
taskRef:
|
||||
name: git-clone
|
||||
workspaces:
|
||||
- name: output
|
||||
workspace: git-source
|
||||
params:
|
||||
- name: url
|
||||
value: $(params.REPO_URL)
|
||||
- name: revision
|
||||
value: $(params.PULL_BASE_SHA)
|
||||
- name: deleteExisting
|
||||
value: "true"
|
||||
- name: build-image
|
||||
taskRef:
|
||||
name: kaniko
|
||||
params:
|
||||
- name: IMAGE
|
||||
value: "$(params.image-name):$(tasks.fetch-repository.results.commit)"
|
||||
- name: CONTEXT
|
||||
value: $(params.path-to-image-context)
|
||||
- name: DOCKERFILE
|
||||
value: $(params.path-to-dockerfile)
|
||||
- name: BUILDER_IMAGE
|
||||
value: "gcr.io/kaniko-project/executor:v1.12.1"
|
||||
- name: EXTRA_ARGS
|
||||
value:
|
||||
- --target=foreign-document-test
|
||||
- --cache=true
|
||||
- --cache-copy-layers
|
||||
- --cache-repo=harbor.fizz.buzz/kanikocache/cache
|
||||
- --use-new-run # Should result in a speed-up
|
||||
- --reproducible # To remove timestamps so layer caching works.
|
||||
- --snapshot-mode=redo
|
||||
- --skip-unused-stages=true
|
||||
- --registry-mirror=dockerhub.dockerhub.svc.cluster.local
|
||||
workspaces:
|
||||
- name: source
|
||||
workspace: git-source
|
||||
- name: dockerconfig
|
||||
workspace: docker-credentials
|
||||
runAfter:
|
||||
- fetch-repository
|
||||
- name: run-image
|
||||
taskRef:
|
||||
name: run-docker-image
|
||||
workspaces:
|
||||
- name: source
|
||||
workspace: git-source
|
||||
- name: cargo-cache
|
||||
workspace: cargo-cache
|
||||
runAfter:
|
||||
- build-image
|
||||
params:
|
||||
- name: docker-image
|
||||
value: "$(params.image-name):$(tasks.fetch-repository.results.commit)"
|
||||
finally:
|
||||
- name: report-success
|
||||
when:
|
||||
- input: "$(tasks.status)"
|
||||
operator: in
|
||||
values: ["Succeeded", "Completed"]
|
||||
taskRef:
|
||||
name: gitea-set-status
|
||||
params:
|
||||
- name: CONTEXT
|
||||
value: "$(params.JOB_NAME)"
|
||||
- name: REPO_FULL_NAME
|
||||
value: "$(params.REPO_OWNER)/$(params.REPO_NAME)"
|
||||
- name: GITEA_HOST_URL
|
||||
value: code.fizz.buzz
|
||||
- name: SHA
|
||||
value: "$(tasks.fetch-repository.results.commit)"
|
||||
- name: DESCRIPTION
|
||||
value: "Build $(params.JOB_NAME) has succeeded"
|
||||
- name: STATE
|
||||
value: success
|
||||
- name: TARGET_URL
|
||||
value: "https://tekton.fizz.buzz/#/namespaces/$(context.pipelineRun.namespace)/pipelineruns/$(context.pipelineRun.name)"
|
||||
- name: report-failure
|
||||
when:
|
||||
- input: "$(tasks.status)"
|
||||
operator: in
|
||||
values: ["Failed"]
|
||||
taskRef:
|
||||
name: gitea-set-status
|
||||
params:
|
||||
- name: CONTEXT
|
||||
value: "$(params.JOB_NAME)"
|
||||
- name: REPO_FULL_NAME
|
||||
value: "$(params.REPO_OWNER)/$(params.REPO_NAME)"
|
||||
- name: GITEA_HOST_URL
|
||||
value: code.fizz.buzz
|
||||
- name: SHA
|
||||
value: "$(tasks.fetch-repository.results.commit)"
|
||||
- name: DESCRIPTION
|
||||
value: "Build $(params.JOB_NAME) has failed"
|
||||
- name: STATE
|
||||
value: failure
|
||||
- name: TARGET_URL
|
||||
value: "https://tekton.fizz.buzz/#/namespaces/$(context.pipelineRun.namespace)/pipelineruns/$(context.pipelineRun.name)"
|
||||
- name: cargo-cache-autoclean
|
||||
taskRef:
|
||||
name: run-docker-image
|
||||
workspaces:
|
||||
- name: source
|
||||
workspace: git-source
|
||||
- name: cargo-cache
|
||||
workspace: cargo-cache
|
||||
params:
|
||||
- name: command
|
||||
value: [cargo, cache, --autoclean]
|
||||
- name: args
|
||||
value: []
|
||||
- name: docker-image
|
||||
value: "$(params.image-name):$(tasks.fetch-repository.results.commit)"
|
||||
workspaces:
|
||||
- name: git-source
|
||||
- name: docker-credentials
|
||||
- name: cargo-cache
|
||||
workspaces:
|
||||
- name: git-source
|
||||
volumeClaimTemplate:
|
||||
spec:
|
||||
storageClassName: "nfs-client"
|
||||
accessModes:
|
||||
- ReadWriteOnce
|
||||
resources:
|
||||
requests:
|
||||
storage: 10Gi
|
||||
subPath: rust-source
|
||||
- name: cargo-cache
|
||||
persistentVolumeClaim:
|
||||
claimName: organic-cargo-cache-test-foreign-document
|
||||
- name: docker-credentials
|
||||
secret:
|
||||
secretName: harbor-plain
|
||||
serviceAccountName: build-bot
|
||||
params:
|
||||
- name: image-name
|
||||
value: "harbor.fizz.buzz/private/organic-test-foreign-document"
|
||||
- name: path-to-image-context
|
||||
value: docker/organic_test/
|
||||
- name: path-to-dockerfile
|
||||
value: docker/organic_test/Dockerfile
|
||||
@@ -83,6 +83,7 @@ spec:
|
||||
value: "gcr.io/kaniko-project/executor:v1.12.1"
|
||||
- name: EXTRA_ARGS
|
||||
value:
|
||||
- --target=tester
|
||||
- --cache=true
|
||||
- --cache-copy-layers
|
||||
- --cache-repo=harbor.fizz.buzz/kanikocache/cache
|
||||
@@ -99,41 +100,29 @@ spec:
|
||||
runAfter:
|
||||
- fetch-repository
|
||||
- name: run-image
|
||||
taskSpec:
|
||||
metadata: {}
|
||||
stepTemplate:
|
||||
name: ""
|
||||
workingDir: "$(workspaces.source.path)"
|
||||
workspaces:
|
||||
- name: source
|
||||
mountPath: /source
|
||||
- name: cargo-cache
|
||||
mountPath: /usr/local/cargo/registry
|
||||
optional: true
|
||||
steps:
|
||||
- name: run
|
||||
image: "$(params.IMAGE)"
|
||||
command: []
|
||||
args:
|
||||
[
|
||||
--no-default-features,
|
||||
--features,
|
||||
compare,
|
||||
--no-fail-fast,
|
||||
--lib,
|
||||
--test,
|
||||
test_loader,
|
||||
]
|
||||
taskRef:
|
||||
name: run-docker-image
|
||||
workspaces:
|
||||
- name: source
|
||||
workspace: git-source
|
||||
- name: cargo-cache
|
||||
workspace: cargo-cache
|
||||
params:
|
||||
- name: IMAGE
|
||||
value: "$(params.image-name):$(tasks.fetch-repository.results.commit)"
|
||||
runAfter:
|
||||
- build-image
|
||||
params:
|
||||
- name: args
|
||||
value:
|
||||
[
|
||||
--no-default-features,
|
||||
--features,
|
||||
compare,
|
||||
--no-fail-fast,
|
||||
--lib,
|
||||
--test,
|
||||
test_loader,
|
||||
]
|
||||
- name: docker-image
|
||||
value: "$(params.image-name):$(tasks.fetch-repository.results.commit)"
|
||||
finally:
|
||||
- name: report-success
|
||||
when:
|
||||
|
||||
@@ -16,6 +16,13 @@ spec:
|
||||
skip_branches:
|
||||
# We already run on every commit, so running when the semver tags get pushed is causing needless double-processing.
|
||||
- "^v[0-9]+\\.[0-9]+\\.[0-9]+$"
|
||||
- name: rust-foreign-document-test
|
||||
source: "pipeline-foreign-document-test.yaml"
|
||||
# Override https-based url from lighthouse events.
|
||||
clone_uri: "git@code.fizz.buzz:talexander/organic.git"
|
||||
skip_branches:
|
||||
# We already run on every commit, so running when the semver tags get pushed is causing needless double-processing.
|
||||
- "^v[0-9]+\\.[0-9]+\\.[0-9]+$"
|
||||
- name: rust-build
|
||||
source: "pipeline-rust-build.yaml"
|
||||
# Override https-based url from lighthouse events.
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
[package]
|
||||
name = "organic"
|
||||
version = "0.1.4"
|
||||
version = "0.1.6"
|
||||
authors = ["Tom Alexander <tom@fizz.buzz>"]
|
||||
description = "An org-mode parser."
|
||||
edition = "2021"
|
||||
@@ -22,7 +22,7 @@ path = "src/lib.rs"
|
||||
|
||||
[[bin]]
|
||||
# This bin exists for development purposes only. The real target of this crate is the library.
|
||||
name = "compare"
|
||||
name = "parse"
|
||||
path = "src/main.rs"
|
||||
|
||||
[dependencies]
|
||||
|
||||
4
Makefile
4
Makefile
@@ -42,6 +42,10 @@ dockertest:
|
||||
> $(MAKE) -C docker/organic_test
|
||||
> docker run --init --rm -i -t --read-only -v "$$(readlink -f ./):/source:ro" --mount type=tmpfs,destination=/tmp --mount source=cargo-cache,target=/usr/local/cargo/registry --mount source=rust-cache,target=/target --env CARGO_TARGET_DIR=/target -w /source organic-test --no-default-features --features compare --no-fail-fast --lib --test test_loader -- --test-threads $(TESTJOBS)
|
||||
|
||||
.PHONY: foreign_document_test
|
||||
foreign_document_test:
|
||||
> $(MAKE) -C docker/organic_test run_foreign_document_test
|
||||
|
||||
.PHONY: dockerclean
|
||||
dockerclean:
|
||||
# Delete volumes created for running the tests in docker. This does not touch anything related to the jaeger docker container.
|
||||
|
||||
4
build.rs
4
build.rs
@@ -72,8 +72,8 @@ fn write_header(test_file: &mut File) {
|
||||
r#"
|
||||
#[feature(exit_status_error)]
|
||||
use organic::compare_document;
|
||||
use organic::parser::document;
|
||||
use organic::emacs_parse_org_document;
|
||||
use organic::parser::parse;
|
||||
use organic::emacs_parse_anonymous_org_document;
|
||||
use organic::parser::sexp::sexp_with_padding;
|
||||
|
||||
"#
|
||||
|
||||
@@ -6,7 +6,7 @@ all: build push
|
||||
|
||||
.PHONY: build
|
||||
build:
|
||||
docker build -t $(IMAGE_NAME) -f Dockerfile ../../
|
||||
docker build -t $(IMAGE_NAME) -f Dockerfile .
|
||||
|
||||
.PHONY: push
|
||||
push:
|
||||
|
||||
@@ -6,7 +6,7 @@ all: build push
|
||||
|
||||
.PHONY: build
|
||||
build:
|
||||
docker build -t $(IMAGE_NAME) -f Dockerfile ../../
|
||||
docker build -t $(IMAGE_NAME) -f Dockerfile .
|
||||
|
||||
.PHONY: push
|
||||
push:
|
||||
|
||||
@@ -14,7 +14,7 @@ RUN make DESTDIR="/root/dist" install
|
||||
|
||||
|
||||
FROM build AS build-org-mode
|
||||
ARG ORG_VERSION=7bdec435ff5d86220d13c431e799c5ed44a57da1
|
||||
ARG ORG_VERSION=163bafb43dcc2bc94a2c7ccaa77d3d1dd488f1af
|
||||
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.
|
||||
@@ -25,7 +25,7 @@ RUN make compile
|
||||
RUN make DESTDIR="/root/dist" install
|
||||
|
||||
|
||||
FROM rustlang/rust:nightly-alpine3.17
|
||||
FROM rustlang/rust:nightly-alpine3.17 AS tester
|
||||
ENV LANG=en_US.UTF-8
|
||||
RUN apk add --no-cache musl-dev ncurses gnutls
|
||||
RUN cargo install --locked --no-default-features --features ci-autoclean cargo-cache
|
||||
@@ -33,3 +33,69 @@ COPY --from=build-emacs /root/dist/ /
|
||||
COPY --from=build-org-mode /root/dist/ /
|
||||
|
||||
ENTRYPOINT ["cargo", "test"]
|
||||
|
||||
|
||||
FROM build as foreign-document-gather
|
||||
|
||||
ARG HOWARD_ABRAMS_DOT_FILES_VERSION=1b54fe75d74670dc7bcbb6b01ea560c45528c628
|
||||
ARG HOWARD_ABRAMS_DOT_FILES_PATH=/foreign_documents/howardabrams/dot-files
|
||||
ARG HOWARD_ABRAMS_DOT_FILES_REPO=https://github.com/howardabrams/dot-files.git
|
||||
RUN mkdir /foreign_documents
|
||||
RUN mkdir -p $HOWARD_ABRAMS_DOT_FILES_PATH && git -C $HOWARD_ABRAMS_DOT_FILES_PATH init --initial-branch=main && git -C $HOWARD_ABRAMS_DOT_FILES_PATH remote add origin $HOWARD_ABRAMS_DOT_FILES_REPO && git -C $HOWARD_ABRAMS_DOT_FILES_PATH fetch origin $HOWARD_ABRAMS_DOT_FILES_VERSION && git -C $HOWARD_ABRAMS_DOT_FILES_PATH checkout FETCH_HEAD
|
||||
|
||||
ARG HOWARD_ABRAMS_HAMACS_VERSION=da51188cc195d41882175d412fe40a8bc5730c5c
|
||||
ARG HOWARD_ABRAMS_HAMACS_PATH=/foreign_documents/howardabrams/hamacs
|
||||
ARG HOWARD_ABRAMS_HAMACS_REPO=https://github.com/howardabrams/hamacs.git
|
||||
RUN mkdir -p $HOWARD_ABRAMS_HAMACS_PATH && git -C $HOWARD_ABRAMS_HAMACS_PATH init --initial-branch=main && git -C $HOWARD_ABRAMS_HAMACS_PATH remote add origin $HOWARD_ABRAMS_HAMACS_REPO && git -C $HOWARD_ABRAMS_HAMACS_PATH fetch origin $HOWARD_ABRAMS_HAMACS_VERSION && git -C $HOWARD_ABRAMS_HAMACS_PATH checkout FETCH_HEAD
|
||||
|
||||
ARG HOWARD_ABRAMS_DEMO_IT_VERSION=e399fd7ceb73caeae7cb50b247359bafcaee2a3f
|
||||
ARG HOWARD_ABRAMS_DEMO_IT_PATH=/foreign_documents/howardabrams/demo-it
|
||||
ARG HOWARD_ABRAMS_DEMO_IT_REPO=https://github.com/howardabrams/demo-it.git
|
||||
RUN mkdir -p $HOWARD_ABRAMS_DEMO_IT_PATH && git -C $HOWARD_ABRAMS_DEMO_IT_PATH init --initial-branch=main && git -C $HOWARD_ABRAMS_DEMO_IT_PATH remote add origin $HOWARD_ABRAMS_DEMO_IT_REPO && git -C $HOWARD_ABRAMS_DEMO_IT_PATH fetch origin $HOWARD_ABRAMS_DEMO_IT_VERSION && git -C $HOWARD_ABRAMS_DEMO_IT_PATH checkout FETCH_HEAD
|
||||
|
||||
ARG HOWARD_ABRAMS_MAGIT_DEMO_VERSION=59e82f6bc7c18f550478d86a8f680c3f2da66985
|
||||
ARG HOWARD_ABRAMS_MAGIT_DEMO_PATH=/foreign_documents/howardabrams/magit-demo
|
||||
ARG HOWARD_ABRAMS_MAGIT_DEMO_REPO=https://github.com/howardabrams/magit-demo.git
|
||||
RUN mkdir -p $HOWARD_ABRAMS_MAGIT_DEMO_PATH && git -C $HOWARD_ABRAMS_MAGIT_DEMO_PATH init --initial-branch=main && git -C $HOWARD_ABRAMS_MAGIT_DEMO_PATH remote add origin $HOWARD_ABRAMS_MAGIT_DEMO_REPO && git -C $HOWARD_ABRAMS_MAGIT_DEMO_PATH fetch origin $HOWARD_ABRAMS_MAGIT_DEMO_VERSION && git -C $HOWARD_ABRAMS_MAGIT_DEMO_PATH checkout FETCH_HEAD
|
||||
|
||||
ARG HOWARD_ABRAMS_PDX_EMACS_HACKERS_VERSION=bfb7bd640fdf0ce3def21f9fc591ed35d776b26d
|
||||
ARG HOWARD_ABRAMS_PDX_EMACS_HACKERS_PATH=/foreign_documents/howardabrams/pdx-emacs-hackers
|
||||
ARG HOWARD_ABRAMS_PDX_EMACS_HACKERS_REPO=https://github.com/howardabrams/pdx-emacs-hackers.git
|
||||
RUN mkdir -p $HOWARD_ABRAMS_PDX_EMACS_HACKERS_PATH && git -C $HOWARD_ABRAMS_PDX_EMACS_HACKERS_PATH init --initial-branch=main && git -C $HOWARD_ABRAMS_PDX_EMACS_HACKERS_PATH remote add origin $HOWARD_ABRAMS_PDX_EMACS_HACKERS_REPO && git -C $HOWARD_ABRAMS_PDX_EMACS_HACKERS_PATH fetch origin $HOWARD_ABRAMS_PDX_EMACS_HACKERS_VERSION && git -C $HOWARD_ABRAMS_PDX_EMACS_HACKERS_PATH checkout FETCH_HEAD
|
||||
|
||||
ARG HOWARD_ABRAMS_FLORA_SIMULATOR_VERSION=50de13068722b9e3878f8598b749b7ccd14e7f8e
|
||||
ARG HOWARD_ABRAMS_FLORA_SIMULATOR_PATH=/foreign_documents/howardabrams/flora-simulator
|
||||
ARG HOWARD_ABRAMS_FLORA_SIMULATOR_REPO=https://github.com/howardabrams/flora-simulator.git
|
||||
RUN mkdir -p $HOWARD_ABRAMS_FLORA_SIMULATOR_PATH && git -C $HOWARD_ABRAMS_FLORA_SIMULATOR_PATH init --initial-branch=main && git -C $HOWARD_ABRAMS_FLORA_SIMULATOR_PATH remote add origin $HOWARD_ABRAMS_FLORA_SIMULATOR_REPO && git -C $HOWARD_ABRAMS_FLORA_SIMULATOR_PATH fetch origin $HOWARD_ABRAMS_FLORA_SIMULATOR_VERSION && git -C $HOWARD_ABRAMS_FLORA_SIMULATOR_PATH checkout FETCH_HEAD
|
||||
|
||||
ARG HOWARD_ABRAMS_LITERATE_DEVOPS_DEMO_VERSION=2d7a5e41001a1adf7ec24aeb6acc8525a72d7892
|
||||
ARG HOWARD_ABRAMS_LITERATE_DEVOPS_DEMO_PATH=/foreign_documents/howardabrams/literate-devops-demo
|
||||
ARG HOWARD_ABRAMS_LITERATE_DEVOPS_DEMO_REPO=https://github.com/howardabrams/literate-devops-demo.git
|
||||
RUN mkdir -p $HOWARD_ABRAMS_LITERATE_DEVOPS_DEMO_PATH && git -C $HOWARD_ABRAMS_LITERATE_DEVOPS_DEMO_PATH init --initial-branch=main && git -C $HOWARD_ABRAMS_LITERATE_DEVOPS_DEMO_PATH remote add origin $HOWARD_ABRAMS_LITERATE_DEVOPS_DEMO_REPO && git -C $HOWARD_ABRAMS_LITERATE_DEVOPS_DEMO_PATH fetch origin $HOWARD_ABRAMS_LITERATE_DEVOPS_DEMO_VERSION && git -C $HOWARD_ABRAMS_LITERATE_DEVOPS_DEMO_PATH checkout FETCH_HEAD
|
||||
|
||||
ARG HOWARD_ABRAMS_CLOJURE_YESQL_XP_VERSION=b651c7f8b47b2710e99fce9652980902bbc1c6c9
|
||||
ARG HOWARD_ABRAMS_CLOJURE_YESQL_XP_PATH=/foreign_documents/howardabrams/clojure-yesql-xp
|
||||
ARG HOWARD_ABRAMS_CLOJURE_YESQL_XP_REPO=https://github.com/howardabrams/clojure-yesql-xp.git
|
||||
RUN mkdir -p $HOWARD_ABRAMS_CLOJURE_YESQL_XP_PATH && git -C $HOWARD_ABRAMS_CLOJURE_YESQL_XP_PATH init --initial-branch=main && git -C $HOWARD_ABRAMS_CLOJURE_YESQL_XP_PATH remote add origin $HOWARD_ABRAMS_CLOJURE_YESQL_XP_REPO && git -C $HOWARD_ABRAMS_CLOJURE_YESQL_XP_PATH fetch origin $HOWARD_ABRAMS_CLOJURE_YESQL_XP_VERSION && git -C $HOWARD_ABRAMS_CLOJURE_YESQL_XP_PATH checkout FETCH_HEAD
|
||||
|
||||
ARG HOWARD_ABRAMS_VEEP_VERSION=e37fcf63a5c4a526255735ee34955528b3b280ae
|
||||
ARG HOWARD_ABRAMS_VEEP_PATH=/foreign_documents/howardabrams/veep
|
||||
ARG HOWARD_ABRAMS_VEEP_REPO=https://github.com/howardabrams/veep.git
|
||||
RUN mkdir -p $HOWARD_ABRAMS_VEEP_PATH && git -C $HOWARD_ABRAMS_VEEP_PATH init --initial-branch=main && git -C $HOWARD_ABRAMS_VEEP_PATH remote add origin $HOWARD_ABRAMS_VEEP_REPO && git -C $HOWARD_ABRAMS_VEEP_PATH fetch origin $HOWARD_ABRAMS_VEEP_VERSION && git -C $HOWARD_ABRAMS_VEEP_PATH checkout FETCH_HEAD
|
||||
|
||||
ARG DOOMEMACS_VERSION=42d5fd83504f8aa80f3248036006fbcd49222943
|
||||
ARG DOOMEMACS_PATH=/foreign_documents/doomemacs
|
||||
ARG DOOMEMACS_REPO=https://github.com/doomemacs/doomemacs.git
|
||||
RUN mkdir -p $DOOMEMACS_PATH && git -C $DOOMEMACS_PATH init --initial-branch=main && git -C $DOOMEMACS_PATH remote add origin $DOOMEMACS_REPO && git -C $DOOMEMACS_PATH fetch origin $DOOMEMACS_VERSION && git -C $DOOMEMACS_PATH checkout FETCH_HEAD
|
||||
|
||||
|
||||
FROM tester as foreign-document-test
|
||||
RUN apk add --no-cache bash coreutils
|
||||
RUN mkdir /foreign_documents
|
||||
COPY --from=build-org-mode /root/org-mode /foreign_documents/org-mode
|
||||
COPY --from=build-emacs /root/emacs /foreign_documents/emacs
|
||||
COPY --from=foreign-document-gather /foreign_documents/howardabrams /foreign_documents/howardabrams
|
||||
COPY --from=foreign-document-gather /foreign_documents/doomemacs /foreign_documents/doomemacs
|
||||
COPY foreign_document_test_entrypoint.sh /entrypoint.sh
|
||||
RUN chmod +x /entrypoint.sh
|
||||
ENTRYPOINT ["/entrypoint.sh"]
|
||||
|
||||
@@ -6,7 +6,11 @@ all: build push
|
||||
|
||||
.PHONY: build
|
||||
build:
|
||||
docker build -t $(IMAGE_NAME) -f Dockerfile ../../
|
||||
docker build -t $(IMAGE_NAME) -f Dockerfile --target tester .
|
||||
|
||||
.PHONY: build_foreign_document_test
|
||||
build_foreign_document_test:
|
||||
docker build -t $(IMAGE_NAME)-foreign-document -f Dockerfile --target foreign-document-test .
|
||||
|
||||
.PHONY: push
|
||||
push:
|
||||
@@ -34,3 +38,7 @@ run: build
|
||||
.PHONY: shell
|
||||
shell: build
|
||||
docker run --rm -i -t --entrypoint /bin/sh --mount type=tmpfs,destination=/tmp -v "$$(readlink -f ../../):/source:ro" --workdir=/source --mount source=cargo-cache,target=/usr/local/cargo/registry --mount source=rust-cache,target=/target --env CARGO_TARGET_DIR=/target $(IMAGE_NAME)
|
||||
|
||||
.PHONY: run_foreign_document_test
|
||||
run_foreign_document_test: build_foreign_document_test
|
||||
docker run --rm --init --read-only --mount type=tmpfs,destination=/tmp -v "$$(readlink -f ../../):/source:ro" --workdir=/source --mount source=cargo-cache,target=/usr/local/cargo/registry --mount source=rust-cache,target=/target --env CARGO_TARGET_DIR=/target $(IMAGE_NAME)-foreign-document
|
||||
|
||||
145
docker/organic_test/foreign_document_test_entrypoint.sh
Normal file
145
docker/organic_test/foreign_document_test_entrypoint.sh
Normal file
@@ -0,0 +1,145 @@
|
||||
#!/usr/bin/env bash
|
||||
#
|
||||
# Run the Organic compare script against a series of documents sourced from exterior places.
|
||||
set -euo pipefail
|
||||
IFS=$'\n\t'
|
||||
DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
|
||||
|
||||
REALPATH=$(command -v uu-realpath || command -v realpath)
|
||||
|
||||
function log {
|
||||
(>&2 echo "${@}")
|
||||
}
|
||||
|
||||
function die {
|
||||
local status_code="$1"
|
||||
shift
|
||||
(>&2 echo "${@}")
|
||||
exit "$status_code"
|
||||
}
|
||||
|
||||
function main {
|
||||
cargo build --no-default-features --features compare --profile release-lto
|
||||
if [ "${CARGO_TARGET_DIR:-}" = "" ]; then
|
||||
CARGO_TARGET_DIR=$(realpath target/)
|
||||
fi
|
||||
PARSE="${CARGO_TARGET_DIR}/release-lto/parse"
|
||||
|
||||
local all_status=0
|
||||
set +e
|
||||
|
||||
(run_compare_function "org-mode" compare_all_org_document "/foreign_documents/org-mode")
|
||||
if [ "$?" -ne 0 ]; then all_status=1; fi
|
||||
(run_compare_function "emacs" compare_all_org_document "/foreign_documents/emacs")
|
||||
if [ "$?" -ne 0 ]; then all_status=1; fi
|
||||
(run_compare_function "howard_abrams" compare_howard_abrams)
|
||||
if [ "$?" -ne 0 ]; then all_status=1; fi
|
||||
(run_compare_function "doomemacs" compare_all_org_document "/foreign_documents/doomemacs")
|
||||
if [ "$?" -ne 0 ]; then all_status=1; fi
|
||||
|
||||
set -e
|
||||
if [ "$all_status" -ne 0 ]; then
|
||||
echo "$(red_text "Some tests failed.")"
|
||||
else
|
||||
echo "$(green_text "All tests passed.")"
|
||||
fi
|
||||
return "$all_status"
|
||||
}
|
||||
|
||||
function green_text {
|
||||
(IFS=' '; printf '\x1b[38;2;0;255;0m%s\x1b[0m' "${*}")
|
||||
}
|
||||
|
||||
function red_text {
|
||||
(IFS=' '; printf '\x1b[38;2;255;0;0m%s\x1b[0m' "${*}")
|
||||
}
|
||||
|
||||
function yellow_text {
|
||||
(IFS=' '; printf '\x1b[38;2;255;255;0m%s\x1b[0m' "${*}")
|
||||
}
|
||||
|
||||
function indent {
|
||||
local depth="$1"
|
||||
local scaled_depth=$((depth * 2))
|
||||
shift 1
|
||||
local prefix=$(printf -- "%${scaled_depth}s")
|
||||
while read l; do
|
||||
(IFS=' '; printf -- '%s%s\n' "$prefix" "$l")
|
||||
done
|
||||
}
|
||||
|
||||
function run_compare_function {
|
||||
local name="$1"
|
||||
local stdoutput
|
||||
shift 1
|
||||
set +e
|
||||
stdoutput=$("${@}")
|
||||
local status=$?
|
||||
set -e
|
||||
if [ "$status" -eq 0 ]; then
|
||||
echo "$(green_text "GOOD") $name"
|
||||
indent 1 <<<"$stdoutput"
|
||||
else
|
||||
echo "$(red_text "FAIL") $name"
|
||||
indent 1 <<<"$stdoutput"
|
||||
return 1
|
||||
fi
|
||||
}
|
||||
|
||||
function compare_all_org_document {
|
||||
local root_dir="$1"
|
||||
local target_document
|
||||
local all_status=0
|
||||
while read target_document; do
|
||||
local relative_path=$($REALPATH --relative-to "$root_dir" "$target_document")
|
||||
set +e
|
||||
(run_compare "$relative_path" "$target_document")
|
||||
if [ "$?" -ne 0 ]; then all_status=1; fi
|
||||
set -e
|
||||
done<<<$(find "$root_dir" -type f -iname '*.org')
|
||||
return "$all_status"
|
||||
}
|
||||
|
||||
function run_compare {
|
||||
local name="$1"
|
||||
local target_document="$2"
|
||||
set +e
|
||||
($PARSE "$target_document" &> /dev/null)
|
||||
local status=$?
|
||||
set -e
|
||||
if [ "$status" -eq 0 ]; then
|
||||
echo "$(green_text "GOOD") $name"
|
||||
else
|
||||
echo "$(red_text "FAIL") $name"
|
||||
return 1
|
||||
fi
|
||||
}
|
||||
|
||||
function compare_howard_abrams {
|
||||
local all_status=0
|
||||
set +e
|
||||
|
||||
(run_compare_function "dot-files" compare_all_org_document "/foreign_documents/howardabrams/dot-files")
|
||||
if [ "$?" -ne 0 ]; then all_status=1; fi
|
||||
(run_compare_function "hamacs" compare_all_org_document "/foreign_documents/howardabrams/hamacs")
|
||||
if [ "$?" -ne 0 ]; then all_status=1; fi
|
||||
(run_compare_function "demo-it" compare_all_org_document "/foreign_documents/howardabrams/demo-it")
|
||||
if [ "$?" -ne 0 ]; then all_status=1; fi
|
||||
(run_compare_function "magit-demo" compare_all_org_document "/foreign_documents/howardabrams/magit-demo")
|
||||
if [ "$?" -ne 0 ]; then all_status=1; fi
|
||||
(run_compare_function "pdx-emacs-hackers" compare_all_org_document "/foreign_documents/howardabrams/pdx-emacs-hackers")
|
||||
if [ "$?" -ne 0 ]; then all_status=1; fi
|
||||
(run_compare_function "flora-simulator" compare_all_org_document "/foreign_documents/howardabrams/flora-simulator")
|
||||
if [ "$?" -ne 0 ]; then all_status=1; fi
|
||||
(run_compare_function "literate-devops-demo" compare_all_org_document "/foreign_documents/howardabrams/literate-devops-demo")
|
||||
if [ "$?" -ne 0 ]; then all_status=1; fi
|
||||
(run_compare_function "clojure-yesql-xp" compare_all_org_document "/foreign_documents/howardabrams/clojure-yesql-xp")
|
||||
if [ "$?" -ne 0 ]; then all_status=1; fi
|
||||
(run_compare_function "veep" compare_all_org_document "/foreign_documents/howardabrams/veep")
|
||||
if [ "$?" -ne 0 ]; then all_status=1; fi
|
||||
|
||||
set -e
|
||||
return "$all_status"
|
||||
}
|
||||
|
||||
main "${@}"
|
||||
1
org_mode_samples/README.org
Normal file
1
org_mode_samples/README.org
Normal file
@@ -0,0 +1 @@
|
||||
This folder contains org-mode documents that get automatically included as tests using build.rs.
|
||||
@@ -0,0 +1,8 @@
|
||||
* Footnotes
|
||||
|
||||
[fn:1]
|
||||
|
||||
#+BEGIN_EXAMPLE
|
||||
baz
|
||||
#+END_EXAMPLE
|
||||
|
||||
@@ -0,0 +1,2 @@
|
||||
3. [@3] foo
|
||||
4. bar
|
||||
@@ -1 +1,2 @@
|
||||
- {{{foo(bar)}}} :: baz
|
||||
- =foo= :: bar
|
||||
|
||||
@@ -0,0 +1,3 @@
|
||||
- foo :: bar
|
||||
- foo :: bar
|
||||
- foo :: bar
|
||||
@@ -0,0 +1 @@
|
||||
- =foo :: bar= :: baz
|
||||
@@ -1,3 +1,5 @@
|
||||
1.
|
||||
2.
|
||||
3.
|
||||
|
||||
* headline
|
||||
|
||||
@@ -0,0 +1,6 @@
|
||||
* Overwrite
|
||||
:PROPERTIES:
|
||||
:header-args: :var foo="lorem"
|
||||
:header-args:emacs-lisp: :var bar="ipsum"
|
||||
:header-args:emacs-lisp+: :results silent :var baz=7
|
||||
:END:
|
||||
@@ -0,0 +1,6 @@
|
||||
src_elisp{(bar)}
|
||||
*src_elisp{(bar)}*
|
||||
|
||||
| foo *bar* |
|
||||
| foo src_elisp{(bar)} |
|
||||
| foo *src_elisp{(bar)}* |
|
||||
8
org_mode_samples/greater_element/table/with_formulas.org
Normal file
8
org_mode_samples/greater_element/table/with_formulas.org
Normal file
@@ -0,0 +1,8 @@
|
||||
| Name | Price | Quantity | Total |
|
||||
|------+-------+----------+-------|
|
||||
| foo | 7 | 4 | 28 |
|
||||
| bar | 3.5 | 3 | 10.5 |
|
||||
|------+-------+----------+-------|
|
||||
| | | 7 | 38.5 |
|
||||
#+tblfm: $4=$2*$3::@>$4=vsum(@2..@-1)
|
||||
#+tblfm: @>$3=vsum(@2..@-1)
|
||||
@@ -0,0 +1,7 @@
|
||||
# This test is to prove that the parser works with affiliated keywords that have both a shorter and longer version.
|
||||
|
||||
#+results:
|
||||
#+result:
|
||||
#+begin_latex
|
||||
\foo
|
||||
#+end_latex
|
||||
1
org_mode_samples/lesser_element/keyword/babel_call.org
Normal file
1
org_mode_samples/lesser_element/keyword/babel_call.org
Normal file
@@ -0,0 +1 @@
|
||||
#+call: foo(bar="baz")
|
||||
@@ -0,0 +1 @@
|
||||
#+title:foo:bar: baz: lorem: ipsum
|
||||
@@ -0,0 +1,2 @@
|
||||
#+begin_src
|
||||
#+end_src
|
||||
@@ -0,0 +1,4 @@
|
||||
# There are trailing spaces after the begin and end src lines
|
||||
#+begin_src
|
||||
echo "this is a source block."
|
||||
#+end_src
|
||||
@@ -0,0 +1,3 @@
|
||||
*[fn:: /abcdef[fn::ghijklmnopqrstuvw]xyz/ r]*
|
||||
|
||||
*[fn:: /abcdef[fn::ghijk *lmnopq* rstuvw]xyz/ r]*
|
||||
@@ -0,0 +1 @@
|
||||
mailto:foo@bar.baz .
|
||||
4
org_mode_samples/object/statistics_cookie/empty.org
Normal file
4
org_mode_samples/object/statistics_cookie/empty.org
Normal file
@@ -0,0 +1,4 @@
|
||||
[/]
|
||||
[/2]
|
||||
[3/]
|
||||
[%]
|
||||
@@ -1 +1,4 @@
|
||||
foo ==>bar=.
|
||||
|
||||
# This uses a zero-width space to escape the equals signs to make the verbatim not end.
|
||||
=lorem == ipsum=
|
||||
|
||||
@@ -0,0 +1,2 @@
|
||||
* TODO [#A] COMMENT foo bar
|
||||
baz
|
||||
@@ -0,0 +1,4 @@
|
||||
* DONE foo
|
||||
DEADLINE: <2023-09-08 Fri>
|
||||
|
||||
* DONE bar
|
||||
@@ -0,0 +1 @@
|
||||
* [0/4] foo
|
||||
@@ -4,10 +4,10 @@ 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
|
||||
|
||||
(cd "$DIR/../" && RUSTFLAGS="-C opt-level=0" cargo build --no-default-features)
|
||||
valgrind --tool=callgrind --callgrind-out-file="$DIR/../callgrind.out" "$DIR/../target/debug/parse" "${@}"
|
||||
|
||||
echo "You probably want to run:"
|
||||
echo "callgrind_annotate --auto=yes callgrind.out"
|
||||
echo "callgrind_annotate --auto=yes '$DIR/../callgrind.out'"
|
||||
|
||||
@@ -6,8 +6,6 @@ DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
|
||||
|
||||
: ${PROFILE:="perf"}
|
||||
|
||||
cd "$DIR/../"
|
||||
|
||||
function main {
|
||||
local additional_flags=()
|
||||
if [ "$PROFILE" = "dev" ] || [ "$PROFILE" = "debug" ]; then
|
||||
@@ -15,12 +13,12 @@ function main {
|
||||
else
|
||||
additional_flags+=(--profile "$PROFILE")
|
||||
fi
|
||||
cargo build --no-default-features "${additional_flags[@]}"
|
||||
perf record --freq=2000 --call-graph dwarf --output=perf.data target/${PROFILE}/compare
|
||||
(cd "$DIR/../" && cargo build --no-default-features "${additional_flags[@]}")
|
||||
perf record --freq=2000 --call-graph dwarf --output="$DIR/../perf.data" "$DIR/../target/${PROFILE}/parse" "${@}"
|
||||
|
||||
# Convert to a format firefox will read
|
||||
# flags to consider --show-info
|
||||
perf script -F +pid --input perf.data > perf.firefox
|
||||
perf script -F +pid --input "$DIR/../perf.data" > "$DIR/../perf.firefox"
|
||||
|
||||
echo "You probably want to go to https://profiler.firefox.com/"
|
||||
echo "Either that or run hotspot"
|
||||
|
||||
@@ -9,14 +9,12 @@ DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
|
||||
: ${BACKTRACE:="NO"} # or YES to print a rust backtrace when panicking
|
||||
: ${NO_COLOR:=""} # Set to anything to disable color output
|
||||
|
||||
|
||||
cd "$DIR/../"
|
||||
REALPATH=$(command -v uu-realpath || command -v realpath)
|
||||
MAKE=$(command -v gmake || command -v make)
|
||||
|
||||
function main {
|
||||
build_container
|
||||
launch_container
|
||||
launch_container "${@}"
|
||||
}
|
||||
|
||||
function build_container {
|
||||
@@ -51,7 +49,17 @@ function launch_container {
|
||||
additional_flags+=(--env RUST_BACKTRACE=full)
|
||||
fi
|
||||
|
||||
docker run "${additional_flags[@]}" --init --rm -i --mount type=tmpfs,destination=/tmp -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 --entrypoint "" organic-test "${additional_args[@]}"
|
||||
if [ $# -gt 0 ]; then
|
||||
# If we passed in args, we need to forward them along
|
||||
for path in "${@}"; do
|
||||
local full_path=$($REALPATH "$path")
|
||||
local containing_folder=$(dirname "$full_path")
|
||||
local file_name=$(basename "$full_path")
|
||||
docker run "${additional_flags[@]}" --init --rm -i --mount type=tmpfs,destination=/tmp -v "${containing_folder}:/input:ro" -v "$($REALPATH "$DIR/../"):/source:ro" --mount source=cargo-cache,target=/usr/local/cargo/registry --mount source=rust-cache,target=/target --env CARGO_TARGET_DIR=/target -w /source --entrypoint "" organic-test "${additional_args[@]}" -- "/input/$file_name"
|
||||
done
|
||||
else
|
||||
docker run "${additional_flags[@]}" --init --rm -i --mount type=tmpfs,destination=/tmp -v "$($REALPATH "$DIR/../"):/source:ro" --mount source=cargo-cache,target=/usr/local/cargo/registry --mount source=rust-cache,target=/target --env CARGO_TARGET_DIR=/target -w /source --entrypoint "" organic-test "${additional_args[@]}"
|
||||
fi
|
||||
}
|
||||
|
||||
main "${@}"
|
||||
|
||||
92
scripts/run_docker_compare_bisect.bash
Executable file
92
scripts/run_docker_compare_bisect.bash
Executable file
@@ -0,0 +1,92 @@
|
||||
#!/usr/bin/env bash
|
||||
#
|
||||
# Bisect parsing a file at various line cut-off points to see which line causes the parse to differ from emacs.
|
||||
set -euo pipefail
|
||||
IFS=$'\n\t'
|
||||
DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
|
||||
|
||||
REALPATH=$(command -v uu-realpath || command -v realpath)
|
||||
|
||||
############## Setup #########################
|
||||
|
||||
function cleanup {
|
||||
for f in "${folders[@]}"; do
|
||||
log "Deleting $f"
|
||||
rm -rf "$f"
|
||||
done
|
||||
}
|
||||
folders=()
|
||||
for sig in EXIT INT QUIT HUP TERM; do
|
||||
trap "set +e; cleanup" "$sig"
|
||||
done
|
||||
|
||||
function die {
|
||||
local status_code="$1"
|
||||
shift
|
||||
(>&2 echo "${@}")
|
||||
exit "$status_code"
|
||||
}
|
||||
|
||||
function log {
|
||||
(>&2 echo "${@}")
|
||||
}
|
||||
|
||||
############## Program #########################
|
||||
|
||||
function main {
|
||||
log "Is is recommended that the output of \`mktemp -d -t 'compare_bisect.XXXXXXXX'\` is inside a tmpfs filesystem since this script will make many writes to these folders."
|
||||
|
||||
local target_full_path=$($REALPATH "$1")
|
||||
SOURCE_FOLDER=$(dirname "$target_full_path")
|
||||
TARGET_DOCUMENT=$(basename "$target_full_path")
|
||||
|
||||
|
||||
local good=0
|
||||
local bad=$(wc -l "$SOURCE_FOLDER/$TARGET_DOCUMENT" | awk '{print $1}')
|
||||
|
||||
set +e
|
||||
run_parse "$bad" &> /dev/null
|
||||
local status=$?
|
||||
set -e
|
||||
if [ $status -eq 0 ]; then
|
||||
log "Entire file passes."
|
||||
exit 0
|
||||
fi
|
||||
|
||||
while [[ "$((bad - good))" -gt 1 ]]; do
|
||||
local next_line=$((((bad - good) / 2) + good))
|
||||
log "Testing line $next_line"
|
||||
set +e
|
||||
run_parse "$next_line" &> /dev/null
|
||||
local status=$?
|
||||
set -e
|
||||
if [ $status -eq 0 ]; then
|
||||
good="$next_line"
|
||||
log "Line $next_line good"
|
||||
else
|
||||
bad="$next_line"
|
||||
log "Line $next_line bad"
|
||||
fi
|
||||
done
|
||||
echo "Bad line: $bad"
|
||||
}
|
||||
|
||||
function setup_temp_dir {
|
||||
local temp_dir=$(mktemp -d -t 'compare_bisect.XXXXXXXX')
|
||||
cp -r "$SOURCE_FOLDER/"* "$temp_dir/"
|
||||
echo "$temp_dir"
|
||||
}
|
||||
|
||||
function run_parse {
|
||||
local lines="$1"
|
||||
local temp_dir=$(setup_temp_dir)
|
||||
folders+=("$temp_dir")
|
||||
cat "$SOURCE_FOLDER/$TARGET_DOCUMENT" | head -n "$lines" > "$temp_dir/$TARGET_DOCUMENT"
|
||||
"${DIR}/run_docker_compare.bash" "$temp_dir/$TARGET_DOCUMENT"
|
||||
local status=$?
|
||||
rm -rf "$temp_dir"
|
||||
# TODO: Remove temp_dir from folders
|
||||
return "$status"
|
||||
}
|
||||
|
||||
main "${@}"
|
||||
@@ -6,7 +6,6 @@ DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
|
||||
|
||||
: ${NO_COLOR:=""} # Set to anything to disable color output
|
||||
|
||||
cd "$DIR/../"
|
||||
REALPATH=$(command -v uu-realpath || command -v realpath)
|
||||
MAKE=$(command -v gmake || command -v make)
|
||||
|
||||
@@ -56,7 +55,7 @@ cargo test --no-default-features --features compare --no-fail-fast --lib --test
|
||||
EOF
|
||||
)
|
||||
|
||||
docker run "${additional_flags[@]}" --init --rm --read-only --mount type=tmpfs,destination=/tmp -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 --entrypoint "" organic-test sh -c "$init_script"
|
||||
docker run "${additional_flags[@]}" --init --rm --read-only --mount type=tmpfs,destination=/tmp -v "$($REALPATH "$DIR/../"):/source:ro" --mount source=cargo-cache,target=/usr/local/cargo/registry --mount source=rust-cache,target=/target --env CARGO_TARGET_DIR=/target -w /source --entrypoint "" organic-test sh -c "$init_script"
|
||||
}
|
||||
|
||||
|
||||
|
||||
@@ -4,7 +4,6 @@ set -euo pipefail
|
||||
IFS=$'\n\t'
|
||||
DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
|
||||
|
||||
cd "$DIR/../"
|
||||
REALPATH=$(command -v uu-realpath || command -v realpath)
|
||||
|
||||
function main {
|
||||
@@ -12,7 +11,7 @@ function main {
|
||||
|
||||
local test
|
||||
while read test; do
|
||||
cargo test --no-default-features --features compare --no-fail-fast --test test_loader "$test" -- --show-output
|
||||
(cd "$DIR/../" && cargo test --no-default-features --features compare --no-fail-fast --test test_loader "$test" -- --show-output)
|
||||
done<<<"$test_names"
|
||||
}
|
||||
|
||||
|
||||
@@ -7,8 +7,6 @@ DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
|
||||
|
||||
: ${PROFILE:="release-lto"}
|
||||
|
||||
cd "$DIR/../"
|
||||
|
||||
function main {
|
||||
local additional_flags=()
|
||||
if [ "$PROFILE" = "dev" ] || [ "$PROFILE" = "debug" ]; then
|
||||
@@ -16,8 +14,8 @@ function main {
|
||||
else
|
||||
additional_flags+=(--profile "$PROFILE")
|
||||
fi
|
||||
cargo build --no-default-features "${additional_flags[@]}"
|
||||
time ./target/${PROFILE}/compare
|
||||
(cd "$DIR/../" && cargo build --no-default-features "${additional_flags[@]}")
|
||||
time "$DIR/../target/${PROFILE}/parse" "${@}"
|
||||
}
|
||||
|
||||
main "${@}"
|
||||
|
||||
@@ -1,3 +1,4 @@
|
||||
use std::collections::BTreeSet;
|
||||
use std::collections::HashSet;
|
||||
|
||||
use super::util::assert_bounds;
|
||||
@@ -5,64 +6,67 @@ use super::util::assert_name;
|
||||
use super::util::get_property;
|
||||
use crate::parser::sexp::unquote;
|
||||
use crate::parser::sexp::Token;
|
||||
use crate::parser::AngleLink;
|
||||
use crate::parser::Bold;
|
||||
use crate::parser::Citation;
|
||||
use crate::parser::CitationReference;
|
||||
use crate::parser::Clock;
|
||||
use crate::parser::Code;
|
||||
use crate::parser::Comment;
|
||||
use crate::parser::CommentBlock;
|
||||
use crate::parser::DiarySexp;
|
||||
use crate::parser::Document;
|
||||
use crate::parser::DocumentElement;
|
||||
use crate::parser::Drawer;
|
||||
use crate::parser::DynamicBlock;
|
||||
use crate::parser::Element;
|
||||
use crate::parser::Entity;
|
||||
use crate::parser::ExampleBlock;
|
||||
use crate::parser::ExportBlock;
|
||||
use crate::parser::ExportSnippet;
|
||||
use crate::parser::FixedWidthArea;
|
||||
use crate::parser::FootnoteDefinition;
|
||||
use crate::parser::FootnoteReference;
|
||||
use crate::parser::GreaterBlock;
|
||||
use crate::parser::Heading;
|
||||
use crate::parser::HorizontalRule;
|
||||
use crate::parser::InlineBabelCall;
|
||||
use crate::parser::InlineSourceBlock;
|
||||
use crate::parser::Italic;
|
||||
use crate::parser::Keyword;
|
||||
use crate::parser::LatexEnvironment;
|
||||
use crate::parser::LatexFragment;
|
||||
use crate::parser::LineBreak;
|
||||
use crate::parser::Object;
|
||||
use crate::parser::OrgMacro;
|
||||
use crate::parser::Paragraph;
|
||||
use crate::parser::PlainLink;
|
||||
use crate::parser::PlainList;
|
||||
use crate::parser::PlainListItem;
|
||||
use crate::parser::PlainText;
|
||||
use crate::parser::Planning;
|
||||
use crate::parser::PropertyDrawer;
|
||||
use crate::parser::RadioLink;
|
||||
use crate::parser::RadioTarget;
|
||||
use crate::parser::RegularLink;
|
||||
use crate::parser::Section;
|
||||
use crate::parser::Source;
|
||||
use crate::parser::SrcBlock;
|
||||
use crate::parser::StatisticsCookie;
|
||||
use crate::parser::StrikeThrough;
|
||||
use crate::parser::Subscript;
|
||||
use crate::parser::Superscript;
|
||||
use crate::parser::Table;
|
||||
use crate::parser::TableCell;
|
||||
use crate::parser::TableRow;
|
||||
use crate::parser::Target;
|
||||
use crate::parser::Timestamp;
|
||||
use crate::parser::Underline;
|
||||
use crate::parser::Verbatim;
|
||||
use crate::parser::VerseBlock;
|
||||
use crate::types::AngleLink;
|
||||
use crate::types::Bold;
|
||||
use crate::types::Citation;
|
||||
use crate::types::CitationReference;
|
||||
use crate::types::Clock;
|
||||
use crate::types::Code;
|
||||
use crate::types::Comment;
|
||||
use crate::types::CommentBlock;
|
||||
use crate::types::DiarySexp;
|
||||
use crate::types::Document;
|
||||
use crate::types::DocumentElement;
|
||||
use crate::types::Drawer;
|
||||
use crate::types::DynamicBlock;
|
||||
use crate::types::Element;
|
||||
use crate::types::Entity;
|
||||
use crate::types::ExampleBlock;
|
||||
use crate::types::ExportBlock;
|
||||
use crate::types::ExportSnippet;
|
||||
use crate::types::FixedWidthArea;
|
||||
use crate::types::FootnoteDefinition;
|
||||
use crate::types::FootnoteReference;
|
||||
use crate::types::GreaterBlock;
|
||||
use crate::types::Heading;
|
||||
use crate::types::HorizontalRule;
|
||||
use crate::types::InlineBabelCall;
|
||||
use crate::types::InlineSourceBlock;
|
||||
use crate::types::Italic;
|
||||
use crate::types::Keyword;
|
||||
use crate::types::LatexEnvironment;
|
||||
use crate::types::LatexFragment;
|
||||
use crate::types::LineBreak;
|
||||
use crate::types::NodeProperty;
|
||||
use crate::types::Object;
|
||||
use crate::types::OrgMacro;
|
||||
use crate::types::Paragraph;
|
||||
use crate::types::PlainLink;
|
||||
use crate::types::PlainList;
|
||||
use crate::types::PlainListItem;
|
||||
use crate::types::PlainText;
|
||||
use crate::types::Planning;
|
||||
use crate::types::PriorityCookie;
|
||||
use crate::types::PropertyDrawer;
|
||||
use crate::types::RadioLink;
|
||||
use crate::types::RadioTarget;
|
||||
use crate::types::RegularLink;
|
||||
use crate::types::Section;
|
||||
use crate::types::Source;
|
||||
use crate::types::SrcBlock;
|
||||
use crate::types::StatisticsCookie;
|
||||
use crate::types::StrikeThrough;
|
||||
use crate::types::Subscript;
|
||||
use crate::types::Superscript;
|
||||
use crate::types::Table;
|
||||
use crate::types::TableCell;
|
||||
use crate::types::TableRow;
|
||||
use crate::types::Target;
|
||||
use crate::types::Timestamp;
|
||||
use crate::types::TodoKeywordType;
|
||||
use crate::types::Underline;
|
||||
use crate::types::Verbatim;
|
||||
use crate::types::VerseBlock;
|
||||
|
||||
#[derive(Debug)]
|
||||
pub enum DiffEntry<'s> {
|
||||
@@ -305,6 +309,7 @@ fn compare_element<'s>(
|
||||
Element::FixedWidthArea(obj) => compare_fixed_width_area(source, emacs, obj),
|
||||
Element::HorizontalRule(obj) => compare_horizontal_rule(source, emacs, obj),
|
||||
Element::Keyword(obj) => compare_keyword(source, emacs, obj),
|
||||
Element::BabelCall(obj) => compare_babel_call(source, emacs, obj),
|
||||
Element::LatexEnvironment(obj) => compare_latex_environment(source, emacs, obj),
|
||||
};
|
||||
match compare_result {
|
||||
@@ -487,7 +492,7 @@ fn compare_heading<'s>(
|
||||
if rust.stars.to_string() != level {
|
||||
this_status = DiffStatus::Bad;
|
||||
message = Some(format!(
|
||||
"Headline level do not much (emacs != rust): {} != {}",
|
||||
"Headline level do not match (emacs != rust): {} != {}",
|
||||
level, rust.stars
|
||||
))
|
||||
}
|
||||
@@ -510,9 +515,9 @@ fn compare_heading<'s>(
|
||||
.map(Token::as_atom)
|
||||
.map_or(Ok(None), |r| r.map(Some))?
|
||||
.unwrap_or("nil");
|
||||
match (todo_keyword, rust.todo_keyword, unquote(todo_keyword)) {
|
||||
match (todo_keyword, &rust.todo_keyword, unquote(todo_keyword)) {
|
||||
("nil", None, _) => {}
|
||||
(_, Some(rust_todo), Ok(emacs_todo)) if emacs_todo == rust_todo => {}
|
||||
(_, Some((_rust_todo_type, rust_todo)), Ok(emacs_todo)) if emacs_todo == *rust_todo => {}
|
||||
(emacs_todo, rust_todo, _) => {
|
||||
this_status = DiffStatus::Bad;
|
||||
message = Some(format!(
|
||||
@@ -521,6 +526,24 @@ fn compare_heading<'s>(
|
||||
));
|
||||
}
|
||||
};
|
||||
// Compare todo-type
|
||||
let todo_type = get_property(emacs, ":todo-type")?
|
||||
.map(Token::as_atom)
|
||||
.map_or(Ok(None), |r| r.map(Some))?
|
||||
.unwrap_or("nil");
|
||||
// todo-type is an unquoted string either todo, done, or nil
|
||||
match (todo_type, &rust.todo_keyword) {
|
||||
("nil", None) => {}
|
||||
("todo", Some((TodoKeywordType::Todo, _))) => {}
|
||||
("done", Some((TodoKeywordType::Done, _))) => {}
|
||||
(emacs_todo, rust_todo) => {
|
||||
this_status = DiffStatus::Bad;
|
||||
message = Some(format!(
|
||||
"(emacs != rust) {:?} != {:?}",
|
||||
emacs_todo, rust_todo
|
||||
));
|
||||
}
|
||||
};
|
||||
|
||||
// Compare title
|
||||
let title = get_property(emacs, ":title")?.ok_or("Missing :title attribute.")?;
|
||||
@@ -532,7 +555,57 @@ fn compare_heading<'s>(
|
||||
.collect::<Result<Vec<_>, _>>()?;
|
||||
child_status.push(artificial_diff_scope("title".to_owned(), title_status)?);
|
||||
|
||||
// TODO: Compare todo-type, priority, :footnote-section-p, :archivedp, :commentedp
|
||||
// Compare priority
|
||||
let priority = get_property(emacs, ":priority")?;
|
||||
match (priority, rust.priority_cookie) {
|
||||
(None, None) => {}
|
||||
(None, Some(_)) | (Some(_), None) => {
|
||||
this_status = DiffStatus::Bad;
|
||||
message = Some(format!(
|
||||
"Priority cookie mismatch (emacs != rust) {:?} != {:?}",
|
||||
priority, rust.priority_cookie
|
||||
));
|
||||
}
|
||||
(Some(emacs_priority_cookie), Some(rust_priority_cookie)) => {
|
||||
let emacs_priority_cookie =
|
||||
emacs_priority_cookie.as_atom()?.parse::<PriorityCookie>()?;
|
||||
if emacs_priority_cookie != rust_priority_cookie {
|
||||
this_status = DiffStatus::Bad;
|
||||
message = Some(format!(
|
||||
"Priority cookie mismatch (emacs != rust) {:?} != {:?}",
|
||||
emacs_priority_cookie, rust_priority_cookie
|
||||
));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Compare archived
|
||||
let archived = get_property(emacs, ":archivedp")?;
|
||||
match (archived, rust.is_archived) {
|
||||
(None, true) | (Some(_), false) => {
|
||||
this_status = DiffStatus::Bad;
|
||||
message = Some(format!(
|
||||
"archived mismatch (emacs != rust) {:?} != {:?}",
|
||||
archived, rust.is_archived
|
||||
));
|
||||
}
|
||||
(None, false) | (Some(_), true) => {}
|
||||
}
|
||||
|
||||
// Compare commented
|
||||
let commented = get_property(emacs, ":commentedp")?;
|
||||
match (commented, rust.is_comment) {
|
||||
(None, true) | (Some(_), false) => {
|
||||
this_status = DiffStatus::Bad;
|
||||
message = Some(format!(
|
||||
"commented mismatch (emacs != rust) {:?} != {:?}",
|
||||
commented, rust.is_comment
|
||||
));
|
||||
}
|
||||
(None, false) | (Some(_), true) => {}
|
||||
}
|
||||
|
||||
// TODO: Compare :footnote-section-p
|
||||
|
||||
// Compare section
|
||||
let section_status = children
|
||||
@@ -714,6 +787,8 @@ fn compare_plain_list_item<'s>(
|
||||
contents_status,
|
||||
)?);
|
||||
|
||||
// TODO: compare :bullet :checkbox :counter :pre-blank
|
||||
|
||||
Ok(DiffResult {
|
||||
status: this_status,
|
||||
name: emacs_name.to_owned(),
|
||||
@@ -914,7 +989,7 @@ fn compare_property_drawer<'s>(
|
||||
rust: &'s PropertyDrawer<'s>,
|
||||
) -> Result<DiffEntry<'s>, Box<dyn std::error::Error>> {
|
||||
let children = emacs.as_list()?;
|
||||
let child_status = Vec::new();
|
||||
let mut child_status = Vec::new();
|
||||
let mut this_status = DiffStatus::Good;
|
||||
let mut message = None;
|
||||
let emacs_name = "property-drawer";
|
||||
@@ -930,9 +1005,8 @@ fn compare_property_drawer<'s>(
|
||||
Ok(_) => {}
|
||||
};
|
||||
|
||||
for (_emacs_child, _rust_child) in children.iter().skip(2).zip(rust.children.iter()) {
|
||||
// TODO: What are node properties and are they the only legal child of property drawers?
|
||||
// child_status.push(compare_element(source, emacs_child, rust_child)?);
|
||||
for (emacs_child, rust_child) in children.iter().skip(2).zip(rust.children.iter()) {
|
||||
child_status.push(compare_node_property(source, emacs_child, rust_child)?);
|
||||
}
|
||||
|
||||
Ok(DiffResult {
|
||||
@@ -946,6 +1020,40 @@ fn compare_property_drawer<'s>(
|
||||
.into())
|
||||
}
|
||||
|
||||
fn compare_node_property<'s>(
|
||||
source: &'s str,
|
||||
emacs: &'s Token<'s>,
|
||||
rust: &'s NodeProperty<'s>,
|
||||
) -> Result<DiffEntry<'s>, Box<dyn std::error::Error>> {
|
||||
let child_status = Vec::new();
|
||||
let mut this_status = DiffStatus::Good;
|
||||
let mut message = None;
|
||||
let emacs_name = "node-property";
|
||||
if assert_name(emacs, emacs_name).is_err() {
|
||||
this_status = DiffStatus::Bad;
|
||||
}
|
||||
|
||||
match assert_bounds(source, emacs, rust) {
|
||||
Err(err) => {
|
||||
this_status = DiffStatus::Bad;
|
||||
message = Some(err.to_string())
|
||||
}
|
||||
Ok(_) => {}
|
||||
};
|
||||
|
||||
// TODO: Compare :key :value
|
||||
|
||||
Ok(DiffResult {
|
||||
status: this_status,
|
||||
name: emacs_name.to_owned(),
|
||||
message,
|
||||
children: child_status,
|
||||
rust_source: rust.get_source(),
|
||||
emacs_token: emacs,
|
||||
}
|
||||
.into())
|
||||
}
|
||||
|
||||
fn compare_table<'s>(
|
||||
source: &'s str,
|
||||
emacs: &'s Token<'s>,
|
||||
@@ -968,6 +1076,44 @@ fn compare_table<'s>(
|
||||
Ok(_) => {}
|
||||
};
|
||||
|
||||
// Compare formulas
|
||||
//
|
||||
// :tblfm is either nil or a list () filled with quoted strings containing the value for any tblfm keywords at the end of the table.
|
||||
let emacs_formulas = get_property(emacs, ":tblfm")?;
|
||||
if let Some(emacs_formulas) = emacs_formulas {
|
||||
let emacs_formulas = emacs_formulas.as_list()?;
|
||||
if emacs_formulas.len() != rust.formulas.len() {
|
||||
this_status = DiffStatus::Bad;
|
||||
message = Some(format!(
|
||||
"Formulas do not match (emacs != rust): {:?} != {:?}",
|
||||
emacs_formulas, rust.formulas
|
||||
))
|
||||
} else {
|
||||
let atoms = emacs_formulas
|
||||
.into_iter()
|
||||
.map(Token::as_atom)
|
||||
.collect::<Result<Vec<_>, _>>()?;
|
||||
let unquoted = atoms
|
||||
.into_iter()
|
||||
.map(unquote)
|
||||
.collect::<Result<BTreeSet<_>, _>>()?;
|
||||
for kw in &rust.formulas {
|
||||
if !unquoted.contains(kw.value) {
|
||||
this_status = DiffStatus::Bad;
|
||||
message = Some(format!("Could not find formula in emacs: {}", kw.value))
|
||||
}
|
||||
}
|
||||
}
|
||||
} else {
|
||||
if !rust.formulas.is_empty() {
|
||||
this_status = DiffStatus::Bad;
|
||||
message = Some(format!(
|
||||
"Formulas do not match (emacs != rust): {:?} != {:?}",
|
||||
emacs_formulas, rust.formulas
|
||||
))
|
||||
}
|
||||
}
|
||||
|
||||
for (emacs_child, rust_child) in children.iter().skip(2).zip(rust.children.iter()) {
|
||||
child_status.push(compare_table_row(source, emacs_child, rust_child)?);
|
||||
}
|
||||
@@ -1005,6 +1151,10 @@ fn compare_table_row<'s>(
|
||||
Ok(_) => {}
|
||||
};
|
||||
|
||||
// TODO: Compare :type
|
||||
//
|
||||
// :type is an unquoted atom of either standard or rule
|
||||
|
||||
for (emacs_child, rust_child) in children.iter().skip(2).zip(rust.children.iter()) {
|
||||
child_status.push(compare_table_cell(source, emacs_child, rust_child)?);
|
||||
}
|
||||
@@ -1203,6 +1353,8 @@ fn compare_src_block<'s>(
|
||||
Ok(_) => {}
|
||||
};
|
||||
|
||||
// TODO: Compare :language :switches :parameters :number-lines :preserve-indent :retain-labels :use-labels :label-fmt :value
|
||||
|
||||
Ok(DiffResult {
|
||||
status: this_status,
|
||||
name: emacs_name.to_owned(),
|
||||
@@ -1392,6 +1544,77 @@ fn compare_keyword<'s>(
|
||||
Ok(_) => {}
|
||||
};
|
||||
|
||||
let key = unquote(
|
||||
get_property(emacs, ":key")?
|
||||
.ok_or("Emacs keywords should have a :key")?
|
||||
.as_atom()?,
|
||||
)?;
|
||||
if key != rust.key.to_uppercase() {
|
||||
this_status = DiffStatus::Bad;
|
||||
message = Some(format!(
|
||||
"Mismatchs keyword keys (emacs != rust) {:?} != {:?}",
|
||||
key, rust.key
|
||||
))
|
||||
}
|
||||
let value = unquote(
|
||||
get_property(emacs, ":value")?
|
||||
.ok_or("Emacs keywords should have a :value")?
|
||||
.as_atom()?,
|
||||
)?;
|
||||
if value != rust.value {
|
||||
this_status = DiffStatus::Bad;
|
||||
message = Some(format!(
|
||||
"Mismatchs keyword values (emacs != rust) {:?} != {:?}",
|
||||
value, rust.value
|
||||
))
|
||||
}
|
||||
|
||||
Ok(DiffResult {
|
||||
status: this_status,
|
||||
name: emacs_name.to_owned(),
|
||||
message,
|
||||
children: child_status,
|
||||
rust_source: rust.get_source(),
|
||||
emacs_token: emacs,
|
||||
}
|
||||
.into())
|
||||
}
|
||||
|
||||
fn compare_babel_call<'s>(
|
||||
source: &'s str,
|
||||
emacs: &'s Token<'s>,
|
||||
rust: &'s Keyword<'s>,
|
||||
) -> Result<DiffEntry<'s>, Box<dyn std::error::Error>> {
|
||||
let child_status = Vec::new();
|
||||
let mut this_status = DiffStatus::Good;
|
||||
let mut message = None;
|
||||
let emacs_name = "babel-call";
|
||||
if assert_name(emacs, emacs_name).is_err() {
|
||||
this_status = DiffStatus::Bad;
|
||||
}
|
||||
|
||||
match assert_bounds(source, emacs, rust) {
|
||||
Err(err) => {
|
||||
this_status = DiffStatus::Bad;
|
||||
message = Some(err.to_string())
|
||||
}
|
||||
Ok(_) => {}
|
||||
};
|
||||
|
||||
// TODO: compare :call :inside-header :arguments :end-header
|
||||
let value = unquote(
|
||||
get_property(emacs, ":value")?
|
||||
.ok_or("Emacs keywords should have a :value")?
|
||||
.as_atom()?,
|
||||
)?;
|
||||
if value != rust.value {
|
||||
this_status = DiffStatus::Bad;
|
||||
message = Some(format!(
|
||||
"Mismatchs keyword values (emacs != rust) {:?} != {:?}",
|
||||
value, rust.value
|
||||
))
|
||||
}
|
||||
|
||||
Ok(DiffResult {
|
||||
status: this_status,
|
||||
name: emacs_name.to_owned(),
|
||||
|
||||
@@ -2,6 +2,7 @@ mod diff;
|
||||
mod parse;
|
||||
mod util;
|
||||
pub use diff::compare_document;
|
||||
pub use parse::emacs_parse_org_document;
|
||||
pub use parse::emacs_parse_anonymous_org_document;
|
||||
pub use parse::emacs_parse_file_org_document;
|
||||
pub use parse::get_emacs_version;
|
||||
pub use parse::get_org_mode_version;
|
||||
|
||||
@@ -1,6 +1,9 @@
|
||||
use std::path::Path;
|
||||
use std::process::Command;
|
||||
|
||||
pub fn emacs_parse_org_document<C>(file_contents: C) -> Result<String, Box<dyn std::error::Error>>
|
||||
pub fn emacs_parse_anonymous_org_document<C>(
|
||||
file_contents: C,
|
||||
) -> Result<String, Box<dyn std::error::Error>>
|
||||
where
|
||||
C: AsRef<str>,
|
||||
{
|
||||
@@ -15,14 +18,46 @@ where
|
||||
escaped_file_contents = escaped_file_contents
|
||||
);
|
||||
let mut cmd = Command::new("emacs");
|
||||
let proc = cmd
|
||||
let cmd = cmd
|
||||
.arg("-q")
|
||||
.arg("--no-site-file")
|
||||
.arg("--no-splash")
|
||||
.arg("--batch")
|
||||
.arg("--eval")
|
||||
.arg(elisp_script);
|
||||
let out = proc.output()?;
|
||||
let out = cmd.output()?;
|
||||
out.status.exit_ok()?;
|
||||
let org_sexp = out.stderr;
|
||||
Ok(String::from_utf8(org_sexp)?)
|
||||
}
|
||||
|
||||
pub fn emacs_parse_file_org_document<P>(file_path: P) -> Result<String, Box<dyn std::error::Error>>
|
||||
where
|
||||
P: AsRef<Path>,
|
||||
{
|
||||
let file_path = file_path.as_ref().canonicalize()?;
|
||||
let containing_directory = file_path.parent().ok_or(format!(
|
||||
"Failed to get containing directory for path {}",
|
||||
file_path.display()
|
||||
))?;
|
||||
let elisp_script = format!(
|
||||
r#"(progn
|
||||
(org-mode)
|
||||
(message "%s" (pp-to-string (org-element-parse-buffer)))
|
||||
)"#
|
||||
);
|
||||
let mut cmd = Command::new("emacs");
|
||||
let cmd = cmd
|
||||
.current_dir(containing_directory)
|
||||
.arg("-q")
|
||||
.arg("--no-site-file")
|
||||
.arg("--no-splash")
|
||||
.arg("--batch")
|
||||
.arg("--insert")
|
||||
.arg(file_path.as_os_str())
|
||||
.arg("--eval")
|
||||
.arg(elisp_script);
|
||||
let out = cmd.output()?;
|
||||
out.status.exit_ok()?;
|
||||
let org_sexp = out.stderr;
|
||||
Ok(String::from_utf8(org_sexp)?)
|
||||
@@ -55,7 +90,7 @@ pub fn get_emacs_version() -> Result<String, Box<dyn std::error::Error>> {
|
||||
(message "%s" (version))
|
||||
)"#;
|
||||
let mut cmd = Command::new("emacs");
|
||||
let proc = cmd
|
||||
let cmd = cmd
|
||||
.arg("-q")
|
||||
.arg("--no-site-file")
|
||||
.arg("--no-splash")
|
||||
@@ -63,7 +98,7 @@ pub fn get_emacs_version() -> Result<String, Box<dyn std::error::Error>> {
|
||||
.arg("--eval")
|
||||
.arg(elisp_script);
|
||||
|
||||
let out = proc.output()?;
|
||||
let out = cmd.output()?;
|
||||
out.status.exit_ok()?;
|
||||
Ok(String::from_utf8(out.stderr)?)
|
||||
}
|
||||
@@ -74,7 +109,7 @@ pub fn get_org_mode_version() -> Result<String, Box<dyn std::error::Error>> {
|
||||
(message "%s" (org-version nil t nil))
|
||||
)"#;
|
||||
let mut cmd = Command::new("emacs");
|
||||
let proc = cmd
|
||||
let cmd = cmd
|
||||
.arg("-q")
|
||||
.arg("--no-site-file")
|
||||
.arg("--no-splash")
|
||||
@@ -82,7 +117,7 @@ pub fn get_org_mode_version() -> Result<String, Box<dyn std::error::Error>> {
|
||||
.arg("--eval")
|
||||
.arg(elisp_script);
|
||||
|
||||
let out = proc.output()?;
|
||||
let out = cmd.output()?;
|
||||
out.status.exit_ok()?;
|
||||
Ok(String::from_utf8(out.stderr)?)
|
||||
}
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
use crate::parser::sexp::Token;
|
||||
use crate::parser::Source;
|
||||
use crate::types::Source;
|
||||
|
||||
/// Check if the child string slice is a slice of the parent string slice.
|
||||
fn is_slice_of(parent: &str, child: &str) -> bool {
|
||||
@@ -141,6 +141,11 @@ fn maybe_token_to_usize(
|
||||
.map_or(Ok(None), |r| r.map(Some))?)
|
||||
}
|
||||
|
||||
/// Get a named property from the emacs token.
|
||||
///
|
||||
/// Returns Ok(None) if value is nil.
|
||||
///
|
||||
/// Returns error if the attribute is not specified on the token at all.
|
||||
pub fn get_property<'s, 'x>(
|
||||
emacs: &'s Token<'s>,
|
||||
key: &'x str,
|
||||
|
||||
184
src/context/context.rs
Normal file
184
src/context/context.rs
Normal file
@@ -0,0 +1,184 @@
|
||||
use std::marker::PhantomData;
|
||||
|
||||
use nom::combinator::eof;
|
||||
use nom::IResult;
|
||||
|
||||
use super::exiting::ExitClass;
|
||||
use super::global_settings::GlobalSettings;
|
||||
use super::list::List;
|
||||
use super::DynContextMatcher;
|
||||
use super::RefContext;
|
||||
use crate::error::CustomError;
|
||||
use crate::error::MyError;
|
||||
use crate::error::Res;
|
||||
use crate::parser::OrgSource;
|
||||
|
||||
#[derive(Debug)]
|
||||
pub enum ContextElement<'r, 's> {
|
||||
/// Stores a parser that indicates that children should exit upon matching an exit matcher.
|
||||
ExitMatcherNode(ExitMatcherNode<'r>),
|
||||
|
||||
/// Stores the name of the current element to prevent directly nesting elements of the same type.
|
||||
Context(&'r str),
|
||||
|
||||
/// Stores the name of the current object to prevent directly nesting elements of the same type.
|
||||
ContextObject(&'r str),
|
||||
|
||||
/// Indicates if elements should consume the whitespace after them.
|
||||
ConsumeTrailingWhitespace(bool),
|
||||
|
||||
/// This is just here to use the 's lifetime until I'm sure we can eliminate it from ContextElement.
|
||||
Placeholder(PhantomData<&'s str>),
|
||||
}
|
||||
|
||||
pub struct ExitMatcherNode<'r> {
|
||||
// TODO: Should this be "&'r DynContextMatcher<'c>" ?
|
||||
pub exit_matcher: &'r DynContextMatcher<'r>,
|
||||
pub class: ExitClass,
|
||||
}
|
||||
|
||||
impl<'r> std::fmt::Debug for ExitMatcherNode<'r> {
|
||||
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
||||
let mut formatter = f.debug_struct("ExitMatcherNode");
|
||||
formatter.field("class", &self.class.to_string());
|
||||
formatter.finish()
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug)]
|
||||
pub struct Context<'g, 'r, 's> {
|
||||
global_settings: &'g GlobalSettings<'g, 's>,
|
||||
tree: List<'r, &'r ContextElement<'r, 's>>,
|
||||
}
|
||||
|
||||
impl<'g, 'r, 's> Context<'g, 'r, 's> {
|
||||
pub fn new(
|
||||
global_settings: &'g GlobalSettings<'g, 's>,
|
||||
tree: List<'r, &'r ContextElement<'r, 's>>,
|
||||
) -> Self {
|
||||
Self {
|
||||
global_settings,
|
||||
tree,
|
||||
}
|
||||
}
|
||||
|
||||
pub fn with_additional_node(&'r self, new_element: &'r ContextElement<'r, 's>) -> Self {
|
||||
let new_tree = self.tree.push(new_element);
|
||||
Self::new(self.global_settings, new_tree)
|
||||
}
|
||||
|
||||
pub fn iter(&'r self) -> super::list::Iter<'r, &'r ContextElement<'r, 's>> {
|
||||
self.tree.iter()
|
||||
}
|
||||
|
||||
pub fn iter_context(&'r self) -> Iter<'g, 'r, 's> {
|
||||
Iter {
|
||||
next: self.tree.iter_list(),
|
||||
global_settings: self.global_settings,
|
||||
}
|
||||
}
|
||||
|
||||
pub fn get_parent(&'r self) -> Option<Self> {
|
||||
self.tree.get_parent().map(|parent_tree| Self {
|
||||
global_settings: self.global_settings,
|
||||
tree: parent_tree.clone(),
|
||||
})
|
||||
}
|
||||
|
||||
pub fn get_data(&self) -> &ContextElement<'r, 's> {
|
||||
self.tree.get_data()
|
||||
}
|
||||
|
||||
pub fn get_global_settings(&self) -> &'g GlobalSettings<'g, 's> {
|
||||
self.global_settings
|
||||
}
|
||||
|
||||
pub fn with_global_settings<'gg>(
|
||||
&self,
|
||||
new_settings: &'gg GlobalSettings<'gg, 's>,
|
||||
) -> Context<'gg, 'r, 's> {
|
||||
Context {
|
||||
global_settings: new_settings,
|
||||
tree: self.tree.clone(),
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg_attr(feature = "tracing", tracing::instrument(ret, level = "debug"))]
|
||||
pub fn check_exit_matcher(
|
||||
&'r self,
|
||||
i: OrgSource<'s>,
|
||||
) -> IResult<OrgSource<'s>, OrgSource<'s>, CustomError<OrgSource<'s>>> {
|
||||
let mut current_class_filter = ExitClass::Delta;
|
||||
for current_node in self.iter_context() {
|
||||
let context_element = current_node.get_data();
|
||||
match context_element {
|
||||
ContextElement::ExitMatcherNode(exit_matcher) => {
|
||||
if exit_matcher.class as u32 <= current_class_filter as u32 {
|
||||
current_class_filter = exit_matcher.class;
|
||||
let local_result = (exit_matcher.exit_matcher)(¤t_node, i);
|
||||
if local_result.is_ok() {
|
||||
return local_result;
|
||||
}
|
||||
}
|
||||
}
|
||||
_ => {}
|
||||
};
|
||||
}
|
||||
// TODO: Make this a specific error instead of just a generic MyError
|
||||
return Err(nom::Err::Error(CustomError::MyError(MyError(
|
||||
"NoExit".into(),
|
||||
))));
|
||||
}
|
||||
|
||||
/// Indicates if elements should consume the whitespace after them.
|
||||
///
|
||||
/// Defaults to true.
|
||||
pub fn should_consume_trailing_whitespace(&self) -> bool {
|
||||
self._should_consume_trailing_whitespace().unwrap_or(true)
|
||||
}
|
||||
|
||||
fn _should_consume_trailing_whitespace(&self) -> Option<bool> {
|
||||
for current_node in self.iter() {
|
||||
match current_node {
|
||||
ContextElement::ConsumeTrailingWhitespace(should) => {
|
||||
return Some(*should);
|
||||
}
|
||||
_ => {}
|
||||
}
|
||||
}
|
||||
None
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg_attr(feature = "tracing", tracing::instrument(ret, level = "debug"))]
|
||||
fn document_end<'b, 'g, 'r, 's>(
|
||||
_context: RefContext<'b, 'g, 'r, 's>,
|
||||
input: OrgSource<'s>,
|
||||
) -> Res<OrgSource<'s>, OrgSource<'s>> {
|
||||
eof(input)
|
||||
}
|
||||
|
||||
pub struct Iter<'g, 'r, 's> {
|
||||
global_settings: &'g GlobalSettings<'g, 's>,
|
||||
next: super::list::IterList<'r, &'r ContextElement<'r, 's>>,
|
||||
}
|
||||
|
||||
impl<'g, 'r, 's> Iterator for Iter<'g, 'r, 's> {
|
||||
type Item = Context<'g, 'r, 's>;
|
||||
|
||||
fn next(&mut self) -> Option<Self::Item> {
|
||||
let next_tree = self.next.next();
|
||||
let ret =
|
||||
next_tree.map(|parent_tree| Context::new(self.global_settings, parent_tree.clone()));
|
||||
ret
|
||||
}
|
||||
}
|
||||
|
||||
impl<'r, 's> ContextElement<'r, 's> {
|
||||
pub fn document_context() -> Self {
|
||||
Self::ExitMatcherNode(ExitMatcherNode {
|
||||
exit_matcher: &document_end,
|
||||
class: ExitClass::Document,
|
||||
})
|
||||
}
|
||||
}
|
||||
14
src/context/exiting.rs
Normal file
14
src/context/exiting.rs
Normal file
@@ -0,0 +1,14 @@
|
||||
#[derive(Debug, Copy, Clone)]
|
||||
pub enum ExitClass {
|
||||
Document = 1,
|
||||
Alpha = 2,
|
||||
Beta = 3,
|
||||
Gamma = 4,
|
||||
Delta = 5,
|
||||
}
|
||||
|
||||
impl std::fmt::Display for ExitClass {
|
||||
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
|
||||
write!(f, "{:?}", self)
|
||||
}
|
||||
}
|
||||
23
src/context/file_access_interface.rs
Normal file
23
src/context/file_access_interface.rs
Normal file
@@ -0,0 +1,23 @@
|
||||
use std::fmt::Debug;
|
||||
use std::path::PathBuf;
|
||||
|
||||
pub trait FileAccessInterface: Debug {
|
||||
fn read_file(&self, path: &str) -> Result<String, std::io::Error>;
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone)]
|
||||
pub struct LocalFileAccessInterface {
|
||||
pub working_directory: Option<PathBuf>,
|
||||
}
|
||||
|
||||
impl FileAccessInterface for LocalFileAccessInterface {
|
||||
fn read_file(&self, path: &str) -> Result<String, std::io::Error> {
|
||||
let final_path = self
|
||||
.working_directory
|
||||
.as_ref()
|
||||
.map(PathBuf::as_path)
|
||||
.map(|pb| pb.join(path))
|
||||
.unwrap_or_else(|| PathBuf::from(path));
|
||||
Ok(std::fs::read_to_string(final_path)?)
|
||||
}
|
||||
}
|
||||
34
src/context/global_settings.rs
Normal file
34
src/context/global_settings.rs
Normal file
@@ -0,0 +1,34 @@
|
||||
use std::collections::BTreeSet;
|
||||
|
||||
use super::FileAccessInterface;
|
||||
use super::LocalFileAccessInterface;
|
||||
use crate::types::Object;
|
||||
|
||||
// TODO: Ultimately, I think we'll need most of this: https://orgmode.org/manual/In_002dbuffer-Settings.html
|
||||
|
||||
#[derive(Debug, Clone)]
|
||||
pub struct GlobalSettings<'g, 's> {
|
||||
pub radio_targets: Vec<&'g Vec<Object<'s>>>,
|
||||
pub file_access: &'g dyn FileAccessInterface,
|
||||
pub in_progress_todo_keywords: BTreeSet<String>,
|
||||
pub complete_todo_keywords: BTreeSet<String>,
|
||||
}
|
||||
|
||||
impl<'g, 's> GlobalSettings<'g, 's> {
|
||||
pub fn new() -> GlobalSettings<'g, 's> {
|
||||
GlobalSettings {
|
||||
radio_targets: Vec::new(),
|
||||
file_access: &LocalFileAccessInterface {
|
||||
working_directory: None,
|
||||
},
|
||||
in_progress_todo_keywords: BTreeSet::new(),
|
||||
complete_todo_keywords: BTreeSet::new(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl<'g, 's> Default for GlobalSettings<'g, 's> {
|
||||
fn default() -> GlobalSettings<'g, 's> {
|
||||
GlobalSettings::new()
|
||||
}
|
||||
}
|
||||
69
src/context/list.rs
Normal file
69
src/context/list.rs
Normal file
@@ -0,0 +1,69 @@
|
||||
use std::fmt::Debug;
|
||||
|
||||
#[derive(Debug, Clone)]
|
||||
pub struct List<'parent, T> {
|
||||
data: T,
|
||||
parent: Link<'parent, T>,
|
||||
}
|
||||
|
||||
type Link<'parent, T> = Option<&'parent List<'parent, T>>;
|
||||
|
||||
impl<'parent, T> List<'parent, T> {
|
||||
pub fn new(first_item: T) -> Self {
|
||||
Self {
|
||||
data: first_item,
|
||||
parent: None,
|
||||
}
|
||||
}
|
||||
|
||||
pub fn get_data(&self) -> &T {
|
||||
&self.data
|
||||
}
|
||||
|
||||
pub fn get_parent(&'parent self) -> Link<'parent, T> {
|
||||
self.parent
|
||||
}
|
||||
|
||||
pub fn iter(&self) -> Iter<'_, T> {
|
||||
Iter { next: Some(self) }
|
||||
}
|
||||
|
||||
pub fn iter_list(&self) -> IterList<'_, T> {
|
||||
IterList { next: Some(self) }
|
||||
}
|
||||
|
||||
pub fn push(&'parent self, item: T) -> Self {
|
||||
Self {
|
||||
data: item,
|
||||
parent: Some(self),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub struct Iter<'a, T> {
|
||||
next: Link<'a, T>,
|
||||
}
|
||||
|
||||
impl<'a, T> Iterator for Iter<'a, T> {
|
||||
type Item = &'a T;
|
||||
|
||||
fn next(&mut self) -> Option<Self::Item> {
|
||||
let ret = self.next.map(|link| link.get_data());
|
||||
self.next = self.next.map(|link| link.get_parent()).flatten();
|
||||
ret
|
||||
}
|
||||
}
|
||||
|
||||
pub struct IterList<'a, T> {
|
||||
next: Link<'a, T>,
|
||||
}
|
||||
|
||||
impl<'a, T> Iterator for IterList<'a, T> {
|
||||
type Item = &'a List<'a, T>;
|
||||
|
||||
fn next(&mut self) -> Option<Self::Item> {
|
||||
let ret = self.next;
|
||||
self.next = self.next.map(|this| this.get_parent()).flatten();
|
||||
ret
|
||||
}
|
||||
}
|
||||
29
src/context/mod.rs
Normal file
29
src/context/mod.rs
Normal file
@@ -0,0 +1,29 @@
|
||||
use crate::error::Res;
|
||||
use crate::parser::OrgSource;
|
||||
|
||||
mod context;
|
||||
mod exiting;
|
||||
mod file_access_interface;
|
||||
mod global_settings;
|
||||
mod list;
|
||||
mod parser_with_context;
|
||||
|
||||
pub type RefContext<'b, 'g, 'r, 's> = &'b Context<'g, 'r, 's>;
|
||||
pub trait ContextMatcher = for<'b, 'g, 'r, 's> Fn(
|
||||
RefContext<'b, 'g, 'r, 's>,
|
||||
OrgSource<'s>,
|
||||
) -> Res<OrgSource<'s>, OrgSource<'s>>;
|
||||
pub type DynContextMatcher<'c> = dyn ContextMatcher + 'c;
|
||||
pub trait Matcher = for<'s> Fn(OrgSource<'s>) -> Res<OrgSource<'s>, OrgSource<'s>>;
|
||||
#[allow(dead_code)]
|
||||
pub type DynMatcher<'c> = dyn Matcher + 'c;
|
||||
|
||||
pub use context::Context;
|
||||
pub use context::ContextElement;
|
||||
pub use context::ExitMatcherNode;
|
||||
pub use exiting::ExitClass;
|
||||
pub use file_access_interface::FileAccessInterface;
|
||||
pub use file_access_interface::LocalFileAccessInterface;
|
||||
pub use global_settings::GlobalSettings;
|
||||
pub use list::List;
|
||||
pub(crate) use parser_with_context::parser_with_context;
|
||||
@@ -5,13 +5,14 @@ use nom::IResult;
|
||||
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)]
|
||||
pub enum CustomError<I> {
|
||||
MyError(MyError<I>),
|
||||
Nom(I, ErrorKind),
|
||||
IO(std::io::Error),
|
||||
}
|
||||
|
||||
#[derive(Debug, PartialEq)]
|
||||
#[derive(Debug)]
|
||||
pub struct MyError<I>(pub I);
|
||||
|
||||
impl<I> ParseError<I> for CustomError<I> {
|
||||
@@ -24,3 +25,9 @@ impl<I> ParseError<I> for CustomError<I> {
|
||||
other
|
||||
}
|
||||
}
|
||||
|
||||
impl<I> From<std::io::Error> for CustomError<I> {
|
||||
fn from(value: std::io::Error) -> Self {
|
||||
CustomError::IO(value)
|
||||
}
|
||||
}
|
||||
|
||||
10
src/lib.rs
10
src/lib.rs
@@ -1,16 +1,24 @@
|
||||
#![feature(round_char_boundary)]
|
||||
#![feature(exit_status_error)]
|
||||
#![feature(trait_alias)]
|
||||
|
||||
#[cfg(feature = "compare")]
|
||||
mod compare;
|
||||
#[cfg(feature = "compare")]
|
||||
pub use compare::compare_document;
|
||||
#[cfg(feature = "compare")]
|
||||
pub use compare::emacs_parse_org_document;
|
||||
pub use compare::emacs_parse_anonymous_org_document;
|
||||
#[cfg(feature = "compare")]
|
||||
pub use compare::emacs_parse_file_org_document;
|
||||
#[cfg(feature = "compare")]
|
||||
pub use compare::get_emacs_version;
|
||||
#[cfg(feature = "compare")]
|
||||
pub use compare::get_org_mode_version;
|
||||
|
||||
mod context;
|
||||
mod error;
|
||||
pub mod parser;
|
||||
pub mod types;
|
||||
|
||||
pub use context::GlobalSettings;
|
||||
pub use context::LocalFileAccessInterface;
|
||||
|
||||
102
src/main.rs
102
src/main.rs
@@ -1,17 +1,24 @@
|
||||
#![feature(round_char_boundary)]
|
||||
#![feature(exact_size_is_empty)]
|
||||
use std::io::Read;
|
||||
use std::path::Path;
|
||||
|
||||
use ::organic::parser::document;
|
||||
use ::organic::parser::parse;
|
||||
#[cfg(feature = "compare")]
|
||||
use organic::compare_document;
|
||||
#[cfg(feature = "compare")]
|
||||
use organic::emacs_parse_org_document;
|
||||
use organic::emacs_parse_anonymous_org_document;
|
||||
#[cfg(feature = "compare")]
|
||||
use organic::emacs_parse_file_org_document;
|
||||
#[cfg(feature = "compare")]
|
||||
use organic::get_emacs_version;
|
||||
#[cfg(feature = "compare")]
|
||||
use organic::get_org_mode_version;
|
||||
use organic::parser::parse_with_settings;
|
||||
#[cfg(feature = "compare")]
|
||||
use organic::parser::sexp::sexp_with_padding;
|
||||
use organic::GlobalSettings;
|
||||
use organic::LocalFileAccessInterface;
|
||||
|
||||
#[cfg(feature = "tracing")]
|
||||
use crate::init_tracing::init_telemetry;
|
||||
@@ -39,8 +46,16 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
|
||||
|
||||
#[cfg_attr(feature = "tracing", tracing::instrument(ret, level = "debug"))]
|
||||
fn main_body() -> Result<(), Box<dyn std::error::Error>> {
|
||||
let org_contents = read_stdin_to_string()?;
|
||||
run_compare(org_contents)
|
||||
let args = std::env::args().skip(1);
|
||||
if args.is_empty() {
|
||||
let org_contents = read_stdin_to_string()?;
|
||||
run_anonymous_parse(org_contents)
|
||||
} else {
|
||||
for arg in args {
|
||||
run_parse_on_file(arg)?
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
fn read_stdin_to_string() -> Result<String, Box<dyn std::error::Error>> {
|
||||
@@ -52,14 +67,12 @@ fn read_stdin_to_string() -> Result<String, Box<dyn std::error::Error>> {
|
||||
}
|
||||
|
||||
#[cfg(feature = "compare")]
|
||||
fn run_compare<P: AsRef<str>>(org_contents: P) -> Result<(), Box<dyn std::error::Error>> {
|
||||
let emacs_version = get_emacs_version()?;
|
||||
let org_mode_version = get_org_mode_version()?;
|
||||
fn run_anonymous_parse<P: AsRef<str>>(org_contents: P) -> Result<(), Box<dyn std::error::Error>> {
|
||||
let org_contents = org_contents.as_ref();
|
||||
eprintln!("Using emacs version: {}", emacs_version.trim());
|
||||
eprintln!("Using org-mode version: {}", org_mode_version.trim());
|
||||
let (remaining, rust_parsed) = document(org_contents).map_err(|e| e.to_string())?;
|
||||
let org_sexp = emacs_parse_org_document(org_contents)?;
|
||||
eprintln!("Using emacs version: {}", get_emacs_version()?.trim());
|
||||
eprintln!("Using org-mode version: {}", get_org_mode_version()?.trim());
|
||||
let rust_parsed = parse(org_contents)?;
|
||||
let org_sexp = emacs_parse_anonymous_org_document(org_contents)?;
|
||||
let (_remaining, parsed_sexp) =
|
||||
sexp_with_padding(org_sexp.as_str()).map_err(|e| e.to_string())?;
|
||||
|
||||
@@ -74,19 +87,78 @@ fn run_compare<P: AsRef<str>>(org_contents: P) -> Result<(), Box<dyn std::error:
|
||||
if diff_result.is_bad() {
|
||||
Err("Diff results do not match.")?;
|
||||
}
|
||||
if remaining != "" {
|
||||
Err(format!("There was unparsed text remaining: {}", remaining))?;
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[cfg(not(feature = "compare"))]
|
||||
fn run_anonymous_parse<P: AsRef<str>>(org_contents: P) -> Result<(), Box<dyn std::error::Error>> {
|
||||
eprintln!(
|
||||
"This program was built with compare disabled. Only parsing with organic, not comparing."
|
||||
);
|
||||
let rust_parsed = parse(org_contents.as_ref())?;
|
||||
println!("{:#?}", rust_parsed);
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[cfg(feature = "compare")]
|
||||
fn run_parse_on_file<P: AsRef<Path>>(org_path: P) -> Result<(), Box<dyn std::error::Error>> {
|
||||
let org_path = org_path.as_ref();
|
||||
eprintln!("Using emacs version: {}", get_emacs_version()?.trim());
|
||||
eprintln!("Using org-mode version: {}", get_org_mode_version()?.trim());
|
||||
let parent_directory = org_path
|
||||
.parent()
|
||||
.ok_or("Should be contained inside a directory.")?;
|
||||
let org_contents = std::fs::read_to_string(org_path)?;
|
||||
let org_contents = org_contents.as_str();
|
||||
let file_access_interface = LocalFileAccessInterface {
|
||||
working_directory: Some(parent_directory.to_path_buf()),
|
||||
};
|
||||
let global_settings = {
|
||||
let mut global_settings = GlobalSettings::default();
|
||||
global_settings.file_access = &file_access_interface;
|
||||
global_settings
|
||||
};
|
||||
let rust_parsed = parse_with_settings(org_contents, &global_settings)?;
|
||||
let org_sexp = emacs_parse_file_org_document(org_path)?;
|
||||
let (_remaining, parsed_sexp) =
|
||||
sexp_with_padding(org_sexp.as_str()).map_err(|e| e.to_string())?;
|
||||
|
||||
println!("{}\n\n\n", org_contents);
|
||||
println!("{}", org_sexp);
|
||||
println!("{:#?}", rust_parsed);
|
||||
|
||||
// We do the diffing after printing out both parsed forms in case the diffing panics
|
||||
let diff_result = compare_document(&parsed_sexp, &rust_parsed)?;
|
||||
diff_result.print(org_contents)?;
|
||||
|
||||
if diff_result.is_bad() {
|
||||
Err("Diff results do not match.")?;
|
||||
}
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[cfg(not(feature = "compare"))]
|
||||
fn run_compare<P: AsRef<str>>(org_contents: P) -> Result<(), Box<dyn std::error::Error>> {
|
||||
fn run_parse_on_file<P: AsRef<Path>>(org_path: P) -> Result<(), Box<dyn std::error::Error>> {
|
||||
let org_path = org_path.as_ref();
|
||||
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())?;
|
||||
let parent_directory = org_path
|
||||
.parent()
|
||||
.ok_or("Should be contained inside a directory.")?;
|
||||
let org_contents = std::fs::read_to_string(org_path)?;
|
||||
let org_contents = org_contents.as_str();
|
||||
let file_access_interface = LocalFileAccessInterface {
|
||||
working_directory: Some(parent_directory.to_path_buf()),
|
||||
};
|
||||
let global_settings = {
|
||||
let mut global_settings = GlobalSettings::default();
|
||||
global_settings.file_access = &file_access_interface;
|
||||
global_settings
|
||||
};
|
||||
let rust_parsed = parse_with_settings(org_contents, &global_settings)?;
|
||||
println!("{:#?}", rust_parsed);
|
||||
Ok(())
|
||||
}
|
||||
|
||||
@@ -6,20 +6,20 @@ use nom::multi::many_till;
|
||||
|
||||
use super::org_source::OrgSource;
|
||||
use super::util::maybe_consume_object_trailing_whitespace_if_not_exiting;
|
||||
use super::Context;
|
||||
use crate::context::parser_with_context;
|
||||
use crate::context::ContextElement;
|
||||
use crate::context::ExitClass;
|
||||
use crate::context::ExitMatcherNode;
|
||||
use crate::context::RefContext;
|
||||
use crate::error::Res;
|
||||
use crate::parser::exiting::ExitClass;
|
||||
use crate::parser::parser_context::ContextElement;
|
||||
use crate::parser::parser_context::ExitMatcherNode;
|
||||
use crate::parser::parser_with_context::parser_with_context;
|
||||
use crate::parser::plain_link::protocol;
|
||||
use crate::parser::util::exit_matcher_parser;
|
||||
use crate::parser::util::get_consumed;
|
||||
use crate::parser::AngleLink;
|
||||
use crate::types::AngleLink;
|
||||
|
||||
#[cfg_attr(feature = "tracing", tracing::instrument(ret, level = "debug"))]
|
||||
pub fn angle_link<'r, 's>(
|
||||
context: Context<'r, 's>,
|
||||
pub fn angle_link<'b, 'g, 'r, 's>(
|
||||
context: RefContext<'b, 'g, 'r, 's>,
|
||||
input: OrgSource<'s>,
|
||||
) -> Res<OrgSource<'s>, AngleLink<'s>> {
|
||||
let (remaining, _) = tag("<")(input)?;
|
||||
@@ -41,15 +41,15 @@ pub fn angle_link<'r, 's>(
|
||||
}
|
||||
|
||||
#[cfg_attr(feature = "tracing", tracing::instrument(ret, level = "debug"))]
|
||||
fn path_angle<'r, 's>(
|
||||
context: Context<'r, 's>,
|
||||
fn path_angle<'b, 'g, 'r, 's>(
|
||||
context: RefContext<'b, 'g, 'r, 's>,
|
||||
input: OrgSource<'s>,
|
||||
) -> Res<OrgSource<'s>, OrgSource<'s>> {
|
||||
let parser_context =
|
||||
context.with_additional_node(ContextElement::ExitMatcherNode(ExitMatcherNode {
|
||||
class: ExitClass::Gamma,
|
||||
exit_matcher: &path_angle_end,
|
||||
}));
|
||||
let parser_context = ContextElement::ExitMatcherNode(ExitMatcherNode {
|
||||
class: ExitClass::Gamma,
|
||||
exit_matcher: &path_angle_end,
|
||||
});
|
||||
let parser_context = context.with_additional_node(&parser_context);
|
||||
|
||||
let exit_matcher = parser_with_context!(exit_matcher_parser)(&parser_context);
|
||||
|
||||
@@ -58,8 +58,8 @@ fn path_angle<'r, 's>(
|
||||
}
|
||||
|
||||
#[cfg_attr(feature = "tracing", tracing::instrument(ret, level = "debug"))]
|
||||
fn path_angle_end<'r, 's>(
|
||||
_context: Context<'r, 's>,
|
||||
fn path_angle_end<'b, 'g, 'r, 's>(
|
||||
_context: RefContext<'b, 'g, 'r, 's>,
|
||||
input: OrgSource<'s>,
|
||||
) -> Res<OrgSource<'s>, OrgSource<'s>> {
|
||||
tag(">")(input)
|
||||
|
||||
@@ -14,24 +14,25 @@ use super::citation_reference::must_balance_bracket;
|
||||
use super::org_source::BracketDepth;
|
||||
use super::org_source::OrgSource;
|
||||
use super::util::maybe_consume_object_trailing_whitespace_if_not_exiting;
|
||||
use super::Context;
|
||||
use crate::context::parser_with_context;
|
||||
use crate::context::ContextElement;
|
||||
use crate::context::ContextMatcher;
|
||||
use crate::context::ExitClass;
|
||||
use crate::context::ExitMatcherNode;
|
||||
use crate::context::RefContext;
|
||||
use crate::error::CustomError;
|
||||
use crate::error::Res;
|
||||
use crate::parser::citation_reference::citation_reference;
|
||||
use crate::parser::citation_reference::citation_reference_key;
|
||||
use crate::parser::exiting::ExitClass;
|
||||
use crate::parser::object::Citation;
|
||||
use crate::parser::object_parser::standard_set_object;
|
||||
use crate::parser::parser_context::ContextElement;
|
||||
use crate::parser::parser_context::ExitMatcherNode;
|
||||
use crate::parser::parser_with_context::parser_with_context;
|
||||
use crate::parser::util::exit_matcher_parser;
|
||||
use crate::parser::util::get_consumed;
|
||||
use crate::parser::Object;
|
||||
use crate::types::Citation;
|
||||
use crate::types::Object;
|
||||
|
||||
#[cfg_attr(feature = "tracing", tracing::instrument(ret, level = "debug"))]
|
||||
pub fn citation<'r, 's>(
|
||||
context: Context<'r, 's>,
|
||||
pub fn citation<'b, 'g, 'r, 's>(
|
||||
context: RefContext<'b, 'g, '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.
|
||||
@@ -60,7 +61,7 @@ pub fn citation<'r, 's>(
|
||||
}
|
||||
|
||||
#[cfg_attr(feature = "tracing", tracing::instrument(ret, level = "debug"))]
|
||||
fn citestyle<'r, 's>(input: OrgSource<'s>) -> Res<OrgSource<'s>, OrgSource<'s>> {
|
||||
fn citestyle<'s>(input: OrgSource<'s>) -> Res<OrgSource<'s>, OrgSource<'s>> {
|
||||
let (remaining, _) = tuple((tag("/"), style))(input)?;
|
||||
let (remaining, _) = opt(tuple((tag("/"), variant)))(remaining)?;
|
||||
let source = get_consumed(input, remaining);
|
||||
@@ -68,30 +69,30 @@ fn citestyle<'r, 's>(input: OrgSource<'s>) -> Res<OrgSource<'s>, OrgSource<'s>>
|
||||
}
|
||||
|
||||
#[cfg_attr(feature = "tracing", tracing::instrument(ret, level = "debug"))]
|
||||
fn style<'r, 's>(input: OrgSource<'s>) -> Res<OrgSource<'s>, OrgSource<'s>> {
|
||||
fn style<'s>(input: OrgSource<'s>) -> Res<OrgSource<'s>, OrgSource<'s>> {
|
||||
recognize(many1(verify(anychar, |c| {
|
||||
c.is_alphanumeric() || "_-".contains(*c)
|
||||
})))(input)
|
||||
}
|
||||
|
||||
#[cfg_attr(feature = "tracing", tracing::instrument(ret, level = "debug"))]
|
||||
fn variant<'r, 's>(input: OrgSource<'s>) -> Res<OrgSource<'s>, OrgSource<'s>> {
|
||||
fn variant<'s>(input: OrgSource<'s>) -> Res<OrgSource<'s>, OrgSource<'s>> {
|
||||
recognize(many1(verify(anychar, |c| {
|
||||
c.is_alphanumeric() || "_-/".contains(*c)
|
||||
})))(input)
|
||||
}
|
||||
|
||||
#[cfg_attr(feature = "tracing", tracing::instrument(ret, level = "debug"))]
|
||||
fn global_prefix<'r, 's>(
|
||||
context: Context<'r, 's>,
|
||||
fn global_prefix<'b, 'g, 'r, 's>(
|
||||
context: RefContext<'b, 'g, 'r, 's>,
|
||||
input: OrgSource<'s>,
|
||||
) -> Res<OrgSource<'s>, Vec<Object<'s>>> {
|
||||
let exit_with_depth = global_prefix_end(input.get_bracket_depth());
|
||||
let parser_context =
|
||||
context.with_additional_node(ContextElement::ExitMatcherNode(ExitMatcherNode {
|
||||
class: ExitClass::Gamma,
|
||||
exit_matcher: &exit_with_depth,
|
||||
}));
|
||||
let parser_context = ContextElement::ExitMatcherNode(ExitMatcherNode {
|
||||
class: ExitClass::Gamma,
|
||||
exit_matcher: &exit_with_depth,
|
||||
});
|
||||
let parser_context = context.with_additional_node(&parser_context);
|
||||
let (remaining, (children, _exit_contents)) = verify(
|
||||
many_till(
|
||||
parser_with_context!(standard_set_object)(&parser_context),
|
||||
@@ -103,17 +104,13 @@ fn global_prefix<'r, 's>(
|
||||
Ok((remaining, children))
|
||||
}
|
||||
|
||||
fn global_prefix_end(
|
||||
starting_bracket_depth: BracketDepth,
|
||||
) -> impl for<'r, 's> Fn(Context<'r, 's>, OrgSource<'s>) -> Res<OrgSource<'s>, OrgSource<'s>> {
|
||||
move |context: Context, input: OrgSource<'_>| {
|
||||
_global_prefix_end(context, input, starting_bracket_depth)
|
||||
}
|
||||
fn global_prefix_end(starting_bracket_depth: BracketDepth) -> impl ContextMatcher {
|
||||
move |context, input: OrgSource<'_>| _global_prefix_end(context, input, starting_bracket_depth)
|
||||
}
|
||||
|
||||
#[cfg_attr(feature = "tracing", tracing::instrument(ret, level = "debug"))]
|
||||
fn _global_prefix_end<'r, 's>(
|
||||
context: Context<'r, 's>,
|
||||
fn _global_prefix_end<'b, 'g, 'r, 's>(
|
||||
context: RefContext<'b, 'g, 'r, 's>,
|
||||
input: OrgSource<'s>,
|
||||
starting_bracket_depth: BracketDepth,
|
||||
) -> Res<OrgSource<'s>, OrgSource<'s>> {
|
||||
@@ -135,16 +132,16 @@ fn _global_prefix_end<'r, 's>(
|
||||
}
|
||||
|
||||
#[cfg_attr(feature = "tracing", tracing::instrument(ret, level = "debug"))]
|
||||
fn global_suffix<'r, 's>(
|
||||
context: Context<'r, 's>,
|
||||
fn global_suffix<'b, 'g, 'r, 's>(
|
||||
context: RefContext<'b, 'g, 'r, 's>,
|
||||
input: OrgSource<'s>,
|
||||
) -> Res<OrgSource<'s>, Vec<Object<'s>>> {
|
||||
let exit_with_depth = global_suffix_end(input.get_bracket_depth());
|
||||
let parser_context =
|
||||
context.with_additional_node(ContextElement::ExitMatcherNode(ExitMatcherNode {
|
||||
class: ExitClass::Gamma,
|
||||
exit_matcher: &exit_with_depth,
|
||||
}));
|
||||
let parser_context = ContextElement::ExitMatcherNode(ExitMatcherNode {
|
||||
class: ExitClass::Gamma,
|
||||
exit_matcher: &exit_with_depth,
|
||||
});
|
||||
let parser_context = context.with_additional_node(&parser_context);
|
||||
let (remaining, (children, _exit_contents)) = verify(
|
||||
many_till(
|
||||
parser_with_context!(standard_set_object)(&parser_context),
|
||||
@@ -155,17 +152,13 @@ fn global_suffix<'r, 's>(
|
||||
Ok((remaining, children))
|
||||
}
|
||||
|
||||
fn global_suffix_end(
|
||||
starting_bracket_depth: BracketDepth,
|
||||
) -> impl for<'r, 's> Fn(Context<'r, 's>, OrgSource<'s>) -> Res<OrgSource<'s>, OrgSource<'s>> {
|
||||
move |context: Context, input: OrgSource<'_>| {
|
||||
_global_suffix_end(context, input, starting_bracket_depth)
|
||||
}
|
||||
fn global_suffix_end(starting_bracket_depth: BracketDepth) -> impl ContextMatcher {
|
||||
move |context, input: OrgSource<'_>| _global_suffix_end(context, input, starting_bracket_depth)
|
||||
}
|
||||
|
||||
#[cfg_attr(feature = "tracing", tracing::instrument(ret, level = "debug"))]
|
||||
fn _global_suffix_end<'r, 's>(
|
||||
context: Context<'r, 's>,
|
||||
fn _global_suffix_end<'b, 'g, 'r, 's>(
|
||||
context: RefContext<'b, 'g, 'r, 's>,
|
||||
input: OrgSource<'s>,
|
||||
starting_bracket_depth: BracketDepth,
|
||||
) -> Res<OrgSource<'s>, OrgSource<'s>> {
|
||||
@@ -189,19 +182,23 @@ fn _global_suffix_end<'r, 's>(
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
use crate::context::Context;
|
||||
use crate::context::GlobalSettings;
|
||||
use crate::context::List;
|
||||
use crate::parser::element_parser::element;
|
||||
use crate::parser::parser_context::ContextTree;
|
||||
use crate::parser::parser_with_context::parser_with_context;
|
||||
use crate::parser::source::Source;
|
||||
use crate::types::Element;
|
||||
use crate::types::Source;
|
||||
|
||||
#[test]
|
||||
fn citation_simple() {
|
||||
let input = OrgSource::new("[cite:@foo]");
|
||||
let initial_context: ContextTree<'_, '_> = ContextTree::new();
|
||||
let global_settings = GlobalSettings::default();
|
||||
let initial_context = ContextElement::document_context();
|
||||
let initial_context = Context::new(&global_settings, List::new(&initial_context));
|
||||
let paragraph_matcher = parser_with_context!(element(true))(&initial_context);
|
||||
let (remaining, first_paragraph) = paragraph_matcher(input).expect("Parse first paragraph");
|
||||
let first_paragraph = match first_paragraph {
|
||||
crate::parser::Element::Paragraph(paragraph) => paragraph,
|
||||
Element::Paragraph(paragraph) => paragraph,
|
||||
_ => panic!("Should be a paragraph!"),
|
||||
};
|
||||
assert_eq!(Into::<&str>::into(remaining), "");
|
||||
|
||||
@@ -12,24 +12,25 @@ use nom::sequence::tuple;
|
||||
|
||||
use super::org_source::BracketDepth;
|
||||
use super::org_source::OrgSource;
|
||||
use super::Context;
|
||||
use crate::context::parser_with_context;
|
||||
use crate::context::ContextElement;
|
||||
use crate::context::ContextMatcher;
|
||||
use crate::context::ExitClass;
|
||||
use crate::context::ExitMatcherNode;
|
||||
use crate::context::RefContext;
|
||||
use crate::error::CustomError;
|
||||
use crate::error::MyError;
|
||||
use crate::error::Res;
|
||||
use crate::parser::exiting::ExitClass;
|
||||
use crate::parser::object::CitationReference;
|
||||
use crate::parser::object_parser::minimal_set_object;
|
||||
use crate::parser::parser_context::ContextElement;
|
||||
use crate::parser::parser_context::ExitMatcherNode;
|
||||
use crate::parser::parser_with_context::parser_with_context;
|
||||
use crate::parser::util::exit_matcher_parser;
|
||||
use crate::parser::util::get_consumed;
|
||||
use crate::parser::util::WORD_CONSTITUENT_CHARACTERS;
|
||||
use crate::parser::Object;
|
||||
use crate::types::CitationReference;
|
||||
use crate::types::Object;
|
||||
|
||||
#[cfg_attr(feature = "tracing", tracing::instrument(ret, level = "debug"))]
|
||||
pub fn citation_reference<'r, 's>(
|
||||
context: Context<'r, 's>,
|
||||
pub fn citation_reference<'b, 'g, 'r, 's>(
|
||||
context: RefContext<'b, 'g, 'r, 's>,
|
||||
input: OrgSource<'s>,
|
||||
) -> Res<OrgSource<'s>, CitationReference<'s>> {
|
||||
let (remaining, _prefix) =
|
||||
@@ -48,8 +49,8 @@ pub fn citation_reference<'r, 's>(
|
||||
}
|
||||
|
||||
#[cfg_attr(feature = "tracing", tracing::instrument(ret, level = "debug"))]
|
||||
pub fn citation_reference_key<'r, 's>(
|
||||
context: Context<'r, 's>,
|
||||
pub fn citation_reference_key<'b, 'g, 'r, 's>(
|
||||
context: RefContext<'b, 'g, 'r, 's>,
|
||||
input: OrgSource<'s>,
|
||||
) -> Res<OrgSource<'s>, OrgSource<'s>> {
|
||||
let (remaining, source) = recognize(tuple((
|
||||
@@ -68,16 +69,16 @@ pub fn citation_reference_key<'r, 's>(
|
||||
}
|
||||
|
||||
#[cfg_attr(feature = "tracing", tracing::instrument(ret, level = "debug"))]
|
||||
fn key_prefix<'r, 's>(
|
||||
context: Context<'r, 's>,
|
||||
fn key_prefix<'b, 'g, 'r, 's>(
|
||||
context: RefContext<'b, 'g, 'r, 's>,
|
||||
input: OrgSource<'s>,
|
||||
) -> Res<OrgSource<'s>, Vec<Object<'s>>> {
|
||||
let exit_with_depth = key_prefix_end(input.get_bracket_depth());
|
||||
let parser_context =
|
||||
context.with_additional_node(ContextElement::ExitMatcherNode(ExitMatcherNode {
|
||||
class: ExitClass::Gamma,
|
||||
exit_matcher: &exit_with_depth,
|
||||
}));
|
||||
let parser_context = ContextElement::ExitMatcherNode(ExitMatcherNode {
|
||||
class: ExitClass::Gamma,
|
||||
exit_matcher: &exit_with_depth,
|
||||
});
|
||||
let parser_context = context.with_additional_node(&parser_context);
|
||||
let (remaining, (children, _exit_contents)) = verify(
|
||||
many_till(
|
||||
parser_with_context!(minimal_set_object)(&parser_context),
|
||||
@@ -89,16 +90,16 @@ fn key_prefix<'r, 's>(
|
||||
}
|
||||
|
||||
#[cfg_attr(feature = "tracing", tracing::instrument(ret, level = "debug"))]
|
||||
fn key_suffix<'r, 's>(
|
||||
context: Context<'r, 's>,
|
||||
fn key_suffix<'b, 'g, 'r, 's>(
|
||||
context: RefContext<'b, 'g, 'r, 's>,
|
||||
input: OrgSource<'s>,
|
||||
) -> Res<OrgSource<'s>, Vec<Object<'s>>> {
|
||||
let exit_with_depth = key_suffix_end(input.get_bracket_depth());
|
||||
let parser_context =
|
||||
context.with_additional_node(ContextElement::ExitMatcherNode(ExitMatcherNode {
|
||||
class: ExitClass::Gamma,
|
||||
exit_matcher: &exit_with_depth,
|
||||
}));
|
||||
let parser_context = ContextElement::ExitMatcherNode(ExitMatcherNode {
|
||||
class: ExitClass::Gamma,
|
||||
exit_matcher: &exit_with_depth,
|
||||
});
|
||||
let parser_context = context.with_additional_node(&parser_context);
|
||||
let (remaining, (children, _exit_contents)) = verify(
|
||||
many_till(
|
||||
parser_with_context!(minimal_set_object)(&parser_context),
|
||||
@@ -109,17 +110,13 @@ fn key_suffix<'r, 's>(
|
||||
Ok((remaining, children))
|
||||
}
|
||||
|
||||
fn key_prefix_end(
|
||||
starting_bracket_depth: BracketDepth,
|
||||
) -> impl for<'r, 's> Fn(Context<'r, 's>, OrgSource<'s>) -> Res<OrgSource<'s>, OrgSource<'s>> {
|
||||
move |context: Context, input: OrgSource<'_>| {
|
||||
_key_prefix_end(context, input, starting_bracket_depth)
|
||||
}
|
||||
fn key_prefix_end(starting_bracket_depth: BracketDepth) -> impl ContextMatcher {
|
||||
move |context, input: OrgSource<'_>| _key_prefix_end(context, input, starting_bracket_depth)
|
||||
}
|
||||
|
||||
#[cfg_attr(feature = "tracing", tracing::instrument(ret, level = "debug"))]
|
||||
fn _key_prefix_end<'r, 's>(
|
||||
context: Context<'r, 's>,
|
||||
fn _key_prefix_end<'b, 'g, 'r, 's>(
|
||||
context: RefContext<'b, 'g, 'r, 's>,
|
||||
input: OrgSource<'s>,
|
||||
starting_bracket_depth: BracketDepth,
|
||||
) -> Res<OrgSource<'s>, OrgSource<'s>> {
|
||||
@@ -140,17 +137,13 @@ fn _key_prefix_end<'r, 's>(
|
||||
))(input)
|
||||
}
|
||||
|
||||
fn key_suffix_end(
|
||||
starting_bracket_depth: BracketDepth,
|
||||
) -> impl for<'r, 's> Fn(Context<'r, 's>, OrgSource<'s>) -> Res<OrgSource<'s>, OrgSource<'s>> {
|
||||
move |context: Context, input: OrgSource<'_>| {
|
||||
_key_suffix_end(context, input, starting_bracket_depth)
|
||||
}
|
||||
fn key_suffix_end(starting_bracket_depth: BracketDepth) -> impl ContextMatcher {
|
||||
move |context, input: OrgSource<'_>| _key_suffix_end(context, input, starting_bracket_depth)
|
||||
}
|
||||
|
||||
#[cfg_attr(feature = "tracing", tracing::instrument(ret, level = "debug"))]
|
||||
fn _key_suffix_end<'r, 's>(
|
||||
_context: Context<'r, 's>,
|
||||
fn _key_suffix_end<'b, 'g, 'r, 's>(
|
||||
_context: RefContext<'b, 'g, 'r, 's>,
|
||||
input: OrgSource<'s>,
|
||||
starting_bracket_depth: BracketDepth,
|
||||
) -> Res<OrgSource<'s>, OrgSource<'s>> {
|
||||
|
||||
@@ -12,16 +12,16 @@ use nom::combinator::verify;
|
||||
use nom::sequence::tuple;
|
||||
|
||||
use super::org_source::OrgSource;
|
||||
use super::Context;
|
||||
use crate::context::parser_with_context;
|
||||
use crate::context::RefContext;
|
||||
use crate::error::Res;
|
||||
use crate::parser::parser_with_context::parser_with_context;
|
||||
use crate::parser::util::get_consumed;
|
||||
use crate::parser::util::start_of_line;
|
||||
use crate::parser::Clock;
|
||||
use crate::types::Clock;
|
||||
|
||||
#[cfg_attr(feature = "tracing", tracing::instrument(ret, level = "debug"))]
|
||||
pub fn clock<'r, 's>(
|
||||
context: Context<'r, 's>,
|
||||
pub fn clock<'b, 'g, 'r, 's>(
|
||||
context: RefContext<'b, 'g, 'r, 's>,
|
||||
input: OrgSource<'s>,
|
||||
) -> Res<OrgSource<'s>, Clock<'s>> {
|
||||
start_of_line(input)?;
|
||||
@@ -44,8 +44,8 @@ pub fn clock<'r, 's>(
|
||||
}
|
||||
|
||||
#[cfg_attr(feature = "tracing", tracing::instrument(ret, level = "debug"))]
|
||||
fn inactive_timestamp_range_duration<'r, 's>(
|
||||
_context: Context<'r, 's>,
|
||||
fn inactive_timestamp_range_duration<'b, 'g, 'r, 's>(
|
||||
_context: RefContext<'b, 'g, 'r, 's>,
|
||||
input: OrgSource<'s>,
|
||||
) -> Res<OrgSource<'s>, OrgSource<'s>> {
|
||||
recognize(tuple((
|
||||
@@ -66,8 +66,8 @@ fn inactive_timestamp_range_duration<'r, 's>(
|
||||
}
|
||||
|
||||
#[cfg_attr(feature = "tracing", tracing::instrument(ret, level = "debug"))]
|
||||
fn inactive_timestamp<'r, 's>(
|
||||
_context: Context<'r, 's>,
|
||||
fn inactive_timestamp<'b, 'g, 'r, 's>(
|
||||
_context: RefContext<'b, 'g, 'r, 's>,
|
||||
input: OrgSource<'s>,
|
||||
) -> Res<OrgSource<'s>, OrgSource<'s>> {
|
||||
recognize(tuple((
|
||||
|
||||
@@ -13,20 +13,20 @@ use nom::sequence::tuple;
|
||||
|
||||
use super::org_source::OrgSource;
|
||||
use super::util::get_consumed;
|
||||
use super::Context;
|
||||
use crate::context::parser_with_context;
|
||||
use crate::context::ContextElement;
|
||||
use crate::context::RefContext;
|
||||
use crate::error::CustomError;
|
||||
use crate::error::MyError;
|
||||
use crate::error::Res;
|
||||
use crate::parser::parser_context::ContextElement;
|
||||
use crate::parser::parser_with_context::parser_with_context;
|
||||
use crate::parser::util::exit_matcher_parser;
|
||||
use crate::parser::util::immediate_in_section;
|
||||
use crate::parser::util::start_of_line;
|
||||
use crate::parser::Comment;
|
||||
use crate::types::Comment;
|
||||
|
||||
#[cfg_attr(feature = "tracing", tracing::instrument(ret, level = "debug"))]
|
||||
pub fn comment<'r, 's>(
|
||||
context: Context<'r, 's>,
|
||||
pub fn comment<'b, 'g, 'r, 's>(
|
||||
context: RefContext<'b, 'g, 'r, 's>,
|
||||
input: OrgSource<'s>,
|
||||
) -> Res<OrgSource<'s>, Comment<'s>> {
|
||||
if immediate_in_section(context, "comment") {
|
||||
@@ -34,7 +34,8 @@ pub fn comment<'r, 's>(
|
||||
"Cannot nest objects of the same element".into(),
|
||||
))));
|
||||
}
|
||||
let parser_context = context.with_additional_node(ContextElement::Context("comment"));
|
||||
let parser_context = ContextElement::Context("comment");
|
||||
let parser_context = context.with_additional_node(&parser_context);
|
||||
let comment_line_matcher = parser_with_context!(comment_line)(&parser_context);
|
||||
let exit_matcher = parser_with_context!(exit_matcher_parser)(&parser_context);
|
||||
let (remaining, _first_line) = comment_line_matcher(input)?;
|
||||
@@ -51,8 +52,8 @@ pub fn comment<'r, 's>(
|
||||
}
|
||||
|
||||
#[cfg_attr(feature = "tracing", tracing::instrument(ret, level = "debug"))]
|
||||
fn comment_line<'r, 's>(
|
||||
_context: Context<'r, 's>,
|
||||
fn comment_line<'b, 'g, 'r, 's>(
|
||||
_context: RefContext<'b, 'g, 'r, 's>,
|
||||
input: OrgSource<'s>,
|
||||
) -> Res<OrgSource<'s>, OrgSource<'s>> {
|
||||
start_of_line(input)?;
|
||||
@@ -69,8 +70,10 @@ fn comment_line<'r, 's>(
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
use crate::parser::parser_context::ContextTree;
|
||||
use crate::parser::parser_with_context::parser_with_context;
|
||||
use crate::context::Context;
|
||||
use crate::context::ContextElement;
|
||||
use crate::context::GlobalSettings;
|
||||
use crate::context::List;
|
||||
|
||||
#[test]
|
||||
fn require_space_after_hash() {
|
||||
@@ -79,7 +82,9 @@ mod tests {
|
||||
#not a comment
|
||||
# Comment again",
|
||||
);
|
||||
let initial_context: ContextTree<'_, '_> = ContextTree::new();
|
||||
let global_settings = GlobalSettings::default();
|
||||
let initial_context = ContextElement::document_context();
|
||||
let initial_context = Context::new(&global_settings, List::new(&initial_context));
|
||||
let comment_matcher = parser_with_context!(comment)(&initial_context);
|
||||
let (remaining, first_comment) = comment_matcher(input).expect("Parse first comment");
|
||||
assert_eq!(
|
||||
|
||||
@@ -11,15 +11,15 @@ use nom::sequence::tuple;
|
||||
|
||||
use super::org_source::OrgSource;
|
||||
use super::sexp::sexp;
|
||||
use super::Context;
|
||||
use crate::context::RefContext;
|
||||
use crate::error::Res;
|
||||
use crate::parser::util::get_consumed;
|
||||
use crate::parser::util::start_of_line;
|
||||
use crate::parser::DiarySexp;
|
||||
use crate::types::DiarySexp;
|
||||
|
||||
#[cfg_attr(feature = "tracing", tracing::instrument(ret, level = "debug"))]
|
||||
pub fn diary_sexp<'r, 's>(
|
||||
_context: Context<'r, 's>,
|
||||
pub fn diary_sexp<'b, 'g, 'r, 's>(
|
||||
_context: RefContext<'b, 'g, 'r, 's>,
|
||||
input: OrgSource<'s>,
|
||||
) -> Res<OrgSource<'s>, DiarySexp<'s>> {
|
||||
start_of_line(input)?;
|
||||
|
||||
@@ -1,112 +1,114 @@
|
||||
use nom::branch::alt;
|
||||
use nom::bytes::complete::tag;
|
||||
use nom::character::complete::anychar;
|
||||
use nom::character::complete::line_ending;
|
||||
use nom::character::complete::space0;
|
||||
use nom::character::complete::space1;
|
||||
use nom::combinator::eof;
|
||||
use nom::combinator::map;
|
||||
use nom::combinator::not;
|
||||
use nom::combinator::all_consuming;
|
||||
use nom::combinator::opt;
|
||||
use nom::combinator::recognize;
|
||||
use nom::combinator::verify;
|
||||
use nom::multi::many0;
|
||||
use nom::multi::many1;
|
||||
use nom::multi::many1_count;
|
||||
use nom::multi::many_till;
|
||||
use nom::multi::separated_list1;
|
||||
use nom::sequence::tuple;
|
||||
|
||||
use super::element::Element;
|
||||
use super::object::Object;
|
||||
use super::org_source::convert_error;
|
||||
use super::headline::heading;
|
||||
use super::in_buffer_settings::apply_in_buffer_settings;
|
||||
use super::in_buffer_settings::scan_for_in_buffer_settings;
|
||||
use super::org_source::OrgSource;
|
||||
use super::parser_with_context::parser_with_context;
|
||||
use super::source::Source;
|
||||
use super::section::zeroth_section;
|
||||
use super::token::AllTokensIterator;
|
||||
use super::token::Token;
|
||||
use super::util::exit_matcher_parser;
|
||||
use super::util::get_consumed;
|
||||
use super::util::start_of_line;
|
||||
use super::Context;
|
||||
use crate::context::parser_with_context;
|
||||
use crate::context::Context;
|
||||
use crate::context::ContextElement;
|
||||
use crate::context::GlobalSettings;
|
||||
use crate::context::List;
|
||||
use crate::context::RefContext;
|
||||
use crate::error::CustomError;
|
||||
use crate::error::MyError;
|
||||
use crate::error::Res;
|
||||
use crate::parser::comment::comment;
|
||||
use crate::parser::element_parser::element;
|
||||
use crate::parser::exiting::ExitClass;
|
||||
use crate::parser::object_parser::standard_set_object;
|
||||
use crate::parser::parser_context::ContextElement;
|
||||
use crate::parser::parser_context::ContextTree;
|
||||
use crate::parser::parser_context::ExitMatcherNode;
|
||||
use crate::parser::planning::planning;
|
||||
use crate::parser::property_drawer::property_drawer;
|
||||
use crate::parser::org_source::convert_error;
|
||||
use crate::parser::util::blank_line;
|
||||
use crate::parser::util::maybe_consume_trailing_whitespace_if_not_exiting;
|
||||
use crate::types::Document;
|
||||
use crate::types::Object;
|
||||
|
||||
#[derive(Debug)]
|
||||
pub struct Document<'s> {
|
||||
pub source: &'s str,
|
||||
pub zeroth_section: Option<Section<'s>>,
|
||||
pub children: Vec<Heading<'s>>,
|
||||
/// Parse a full org-mode document.
|
||||
///
|
||||
/// This is the main entry point for Organic. It will parse the full contents of the input string as an org-mode document.
|
||||
#[allow(dead_code)]
|
||||
pub fn parse<'s>(input: &'s str) -> Result<Document<'s>, String> {
|
||||
parse_with_settings(input, &GlobalSettings::default())
|
||||
}
|
||||
|
||||
#[derive(Debug)]
|
||||
pub struct Heading<'s> {
|
||||
pub source: &'s str,
|
||||
pub stars: usize,
|
||||
pub todo_keyword: Option<&'s str>,
|
||||
// TODO: add todo-type enum
|
||||
pub title: Vec<Object<'s>>,
|
||||
pub tags: Vec<&'s str>,
|
||||
pub children: Vec<DocumentElement<'s>>,
|
||||
/// Parse a full org-mode document with starting settings.
|
||||
///
|
||||
/// This is the secondary entry point for Organic. It will parse the full contents of the input string as an org-mode document starting with the settings you supplied.
|
||||
///
|
||||
/// This will not prevent additional settings from being learned during parsing, for example when encountering a "#+TODO".
|
||||
#[allow(dead_code)]
|
||||
pub fn parse_with_settings<'g, 's>(
|
||||
input: &'s str,
|
||||
global_settings: &'g GlobalSettings<'g, 's>,
|
||||
) -> Result<Document<'s>, String> {
|
||||
let initial_context = ContextElement::document_context();
|
||||
let initial_context = Context::new(global_settings, List::new(&initial_context));
|
||||
let wrapped_input = OrgSource::new(input);
|
||||
let ret =
|
||||
all_consuming(parser_with_context!(document_org_source)(&initial_context))(wrapped_input)
|
||||
.map_err(|err| err.to_string())
|
||||
.map(|(_remaining, parsed_document)| parsed_document);
|
||||
ret
|
||||
}
|
||||
|
||||
#[derive(Debug)]
|
||||
pub struct Section<'s> {
|
||||
pub source: &'s str,
|
||||
pub children: Vec<Element<'s>>,
|
||||
}
|
||||
|
||||
#[derive(Debug)]
|
||||
pub enum DocumentElement<'s> {
|
||||
Heading(Heading<'s>),
|
||||
Section(Section<'s>),
|
||||
}
|
||||
|
||||
impl<'s> Source<'s> for Document<'s> {
|
||||
fn get_source(&'s self) -> &'s str {
|
||||
self.source
|
||||
}
|
||||
}
|
||||
|
||||
impl<'s> Source<'s> for DocumentElement<'s> {
|
||||
fn get_source(&'s self) -> &'s str {
|
||||
match self {
|
||||
DocumentElement::Heading(obj) => obj.source,
|
||||
DocumentElement::Section(obj) => obj.source,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl<'s> Source<'s> for Section<'s> {
|
||||
fn get_source(&'s self) -> &'s str {
|
||||
self.source
|
||||
}
|
||||
}
|
||||
|
||||
impl<'s> Source<'s> for Heading<'s> {
|
||||
fn get_source(&'s self) -> &'s str {
|
||||
self.source
|
||||
}
|
||||
/// Parse a full org-mode document.
|
||||
///
|
||||
/// Use this entry point when you want to have direct control over the starting context or if you want to use this integrated with other nom parsers. For general-purpose usage, the `parse` and `parse_with_settings` functions are a lot simpler.
|
||||
///
|
||||
/// This will not prevent additional settings from being learned during parsing, for example when encountering a "#+TODO".
|
||||
#[allow(dead_code)]
|
||||
pub fn document<'b, 'g, 'r, 's>(
|
||||
context: RefContext<'b, 'g, 'r, 's>,
|
||||
input: &'s str,
|
||||
) -> Res<&'s str, Document<'s>> {
|
||||
let (remaining, doc) = document_org_source(context, input.into()).map_err(convert_error)?;
|
||||
Ok((Into::<&str>::into(remaining), doc))
|
||||
}
|
||||
|
||||
#[cfg_attr(feature = "tracing", tracing::instrument(ret, level = "debug"))]
|
||||
#[allow(dead_code)]
|
||||
pub fn document(input: &str) -> Res<&str, Document> {
|
||||
let initial_context: ContextTree<'_, '_> = ContextTree::new();
|
||||
let wrapped_input = OrgSource::new(input);
|
||||
let (remaining, document) = _document(&initial_context, wrapped_input)
|
||||
.map(|(rem, out)| (Into::<&str>::into(rem), out))
|
||||
.map_err(convert_error)?;
|
||||
fn document_org_source<'b, 'g, 'r, 's>(
|
||||
context: RefContext<'b, 'g, 'r, 's>,
|
||||
input: OrgSource<'s>,
|
||||
) -> Res<OrgSource<'s>, Document<'s>> {
|
||||
let mut final_settings = Vec::new();
|
||||
let (_, document_settings) = scan_for_in_buffer_settings(input)?;
|
||||
let setup_files: Vec<String> = document_settings
|
||||
.iter()
|
||||
.filter(|kw| kw.key.eq_ignore_ascii_case("setupfile"))
|
||||
.map(|kw| kw.value)
|
||||
.map(|setup_file| {
|
||||
context
|
||||
.get_global_settings()
|
||||
.file_access
|
||||
.read_file(setup_file)
|
||||
.map_err(|err| nom::Err::<CustomError<OrgSource<'_>>>::Failure(err.into()))
|
||||
})
|
||||
.collect::<Result<Vec<_>, _>>()?;
|
||||
for setup_file in setup_files.iter().map(String::as_str) {
|
||||
let (_, setup_file_settings) =
|
||||
scan_for_in_buffer_settings(setup_file.into()).map_err(|_err| {
|
||||
nom::Err::Error(CustomError::MyError(MyError(
|
||||
"TODO: make this take an owned string so I can dump err.to_string() into it."
|
||||
.into(),
|
||||
)))
|
||||
})?;
|
||||
final_settings.extend(setup_file_settings);
|
||||
}
|
||||
final_settings.extend(document_settings);
|
||||
let new_settings = apply_in_buffer_settings(final_settings, context.get_global_settings())
|
||||
.map_err(|_err| {
|
||||
nom::Err::Error(CustomError::MyError(MyError(
|
||||
"TODO: make this take an owned string so I can dump err.to_string() into it."
|
||||
.into(),
|
||||
)))
|
||||
})?;
|
||||
let new_context = context.with_global_settings(&new_settings);
|
||||
let context = &new_context;
|
||||
|
||||
let (remaining, document) =
|
||||
_document(context, input).map(|(rem, out)| (Into::<&str>::into(rem), out))?;
|
||||
{
|
||||
// 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
|
||||
@@ -122,11 +124,11 @@ pub fn document(input: &str) -> Res<&str, Document> {
|
||||
.map(|rt| &rt.children)
|
||||
.collect();
|
||||
if !all_radio_targets.is_empty() {
|
||||
let initial_context = initial_context
|
||||
.with_additional_node(ContextElement::RadioTarget(all_radio_targets));
|
||||
let (remaining, document) = _document(&initial_context, wrapped_input)
|
||||
.map(|(rem, out)| (Into::<&str>::into(rem), out))
|
||||
.map_err(convert_error)?;
|
||||
let mut new_global_settings = context.get_global_settings().clone();
|
||||
new_global_settings.radio_targets = all_radio_targets;
|
||||
let parser_context = context.with_global_settings(&new_global_settings);
|
||||
let (remaining, document) = _document(&parser_context, input)
|
||||
.map(|(rem, out)| (Into::<&str>::into(rem), out))?;
|
||||
return Ok((remaining.into(), document));
|
||||
}
|
||||
}
|
||||
@@ -134,8 +136,8 @@ pub fn document(input: &str) -> Res<&str, Document> {
|
||||
}
|
||||
|
||||
#[cfg_attr(feature = "tracing", tracing::instrument(ret, level = "debug"))]
|
||||
fn _document<'r, 's>(
|
||||
context: Context<'r, 's>,
|
||||
fn _document<'b, 'g, 'r, 's>(
|
||||
context: RefContext<'b, 'g, 'r, 's>,
|
||||
input: OrgSource<'s>,
|
||||
) -> Res<OrgSource<'s>, Document<'s>> {
|
||||
let zeroth_section_matcher = parser_with_context!(zeroth_section)(context);
|
||||
@@ -154,257 +156,6 @@ fn _document<'r, 's>(
|
||||
))
|
||||
}
|
||||
|
||||
#[cfg_attr(feature = "tracing", tracing::instrument(ret, level = "debug"))]
|
||||
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
|
||||
let parser_context = context
|
||||
.with_additional_node(ContextElement::ConsumeTrailingWhitespace(true))
|
||||
.with_additional_node(ContextElement::Context("section"))
|
||||
.with_additional_node(ContextElement::ExitMatcherNode(ExitMatcherNode {
|
||||
class: ExitClass::Document,
|
||||
exit_matcher: §ion_end,
|
||||
}));
|
||||
let without_consuming_whitespace_context =
|
||||
parser_context.with_additional_node(ContextElement::ConsumeTrailingWhitespace(false));
|
||||
|
||||
let element_matcher = parser_with_context!(element(true))(&parser_context);
|
||||
let exit_matcher = parser_with_context!(exit_matcher_parser)(&parser_context);
|
||||
|
||||
let (remaining, comment_and_property_drawer_element) = opt(tuple((
|
||||
opt(parser_with_context!(comment)(
|
||||
&without_consuming_whitespace_context,
|
||||
)),
|
||||
parser_with_context!(property_drawer)(context),
|
||||
many0(blank_line),
|
||||
)))(input)?;
|
||||
|
||||
let (remaining, (mut children, _exit_contents)) = verify(
|
||||
many_till(element_matcher, exit_matcher),
|
||||
|(children, _exit_contents)| {
|
||||
!children.is_empty() || comment_and_property_drawer_element.is_some()
|
||||
},
|
||||
)(remaining)?;
|
||||
|
||||
comment_and_property_drawer_element.map(|(comment, property_drawer, _ws)| {
|
||||
children.insert(0, Element::PropertyDrawer(property_drawer));
|
||||
comment
|
||||
.map(Element::Comment)
|
||||
.map(|ele| children.insert(0, ele));
|
||||
});
|
||||
|
||||
let (remaining, _trailing_ws) =
|
||||
maybe_consume_trailing_whitespace_if_not_exiting(context, remaining)?;
|
||||
|
||||
let source = get_consumed(input, remaining);
|
||||
Ok((
|
||||
remaining,
|
||||
Section {
|
||||
source: source.into(),
|
||||
children,
|
||||
},
|
||||
))
|
||||
}
|
||||
|
||||
#[cfg_attr(feature = "tracing", tracing::instrument(ret, level = "debug"))]
|
||||
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
|
||||
let parser_context = context
|
||||
.with_additional_node(ContextElement::ConsumeTrailingWhitespace(true))
|
||||
.with_additional_node(ContextElement::Context("section"))
|
||||
.with_additional_node(ContextElement::ExitMatcherNode(ExitMatcherNode {
|
||||
class: ExitClass::Document,
|
||||
exit_matcher: §ion_end,
|
||||
}));
|
||||
let element_matcher = parser_with_context!(element(true))(&parser_context);
|
||||
let exit_matcher = parser_with_context!(exit_matcher_parser)(&parser_context);
|
||||
let (mut remaining, (planning_element, property_drawer_element)) = tuple((
|
||||
opt(parser_with_context!(planning)(&parser_context)),
|
||||
opt(parser_with_context!(property_drawer)(&parser_context)),
|
||||
))(input)?;
|
||||
if planning_element.is_none() && property_drawer_element.is_none() {
|
||||
let (remain, _ws) = many0(blank_line)(remaining)?;
|
||||
remaining = remain;
|
||||
input = remain;
|
||||
}
|
||||
let (remaining, (mut children, _exit_contents)) = verify(
|
||||
many_till(element_matcher, exit_matcher),
|
||||
|(children, _exit_contents)| {
|
||||
!children.is_empty() || property_drawer_element.is_some() || planning_element.is_some()
|
||||
},
|
||||
)(remaining)?;
|
||||
property_drawer_element
|
||||
.map(Element::PropertyDrawer)
|
||||
.map(|ele| children.insert(0, ele));
|
||||
planning_element
|
||||
.map(Element::Planning)
|
||||
.map(|ele| children.insert(0, ele));
|
||||
|
||||
let (remaining, _trailing_ws) =
|
||||
maybe_consume_trailing_whitespace_if_not_exiting(context, remaining)?;
|
||||
|
||||
let source = get_consumed(input, remaining);
|
||||
Ok((
|
||||
remaining,
|
||||
Section {
|
||||
source: source.into(),
|
||||
children,
|
||||
},
|
||||
))
|
||||
}
|
||||
|
||||
#[cfg_attr(feature = "tracing", tracing::instrument(ret, level = "debug"))]
|
||||
fn section_end<'r, 's>(
|
||||
_context: Context<'r, 's>,
|
||||
input: OrgSource<'s>,
|
||||
) -> Res<OrgSource<'s>, OrgSource<'s>> {
|
||||
recognize(detect_headline)(input)
|
||||
}
|
||||
|
||||
const fn heading(
|
||||
parent_stars: usize,
|
||||
) -> impl for<'r, 's> Fn(Context<'r, 's>, OrgSource<'s>) -> Res<OrgSource<'s>, Heading<'s>> {
|
||||
move |context: Context, input: OrgSource<'_>| _heading(context, input, parent_stars)
|
||||
}
|
||||
|
||||
#[cfg_attr(feature = "tracing", tracing::instrument(ret, level = "debug"))]
|
||||
fn _heading<'r, 's>(
|
||||
context: Context<'r, 's>,
|
||||
input: OrgSource<'s>,
|
||||
parent_stars: usize,
|
||||
) -> Res<OrgSource<'s>, Heading<'s>> {
|
||||
not(|i| context.check_exit_matcher(i))(input)?;
|
||||
let (remaining, (star_count, _ws, maybe_todo_keyword, title, heading_tags)) =
|
||||
headline(context, input, parent_stars)?;
|
||||
let section_matcher = parser_with_context!(section)(context);
|
||||
let heading_matcher = parser_with_context!(heading(star_count))(context);
|
||||
let (remaining, maybe_section) =
|
||||
opt(map(section_matcher, DocumentElement::Section))(remaining)?;
|
||||
let (remaining, mut children) =
|
||||
many0(map(heading_matcher, DocumentElement::Heading))(remaining)?;
|
||||
if let Some(section) = maybe_section {
|
||||
children.insert(0, section);
|
||||
}
|
||||
let remaining = if children.is_empty() {
|
||||
// Support empty headings
|
||||
let (remain, _ws) = many0(blank_line)(remaining)?;
|
||||
remain
|
||||
} else {
|
||||
remaining
|
||||
};
|
||||
|
||||
let source = get_consumed(input, remaining);
|
||||
Ok((
|
||||
remaining,
|
||||
Heading {
|
||||
source: source.into(),
|
||||
stars: star_count,
|
||||
todo_keyword: maybe_todo_keyword
|
||||
.map(|(todo_keyword, _ws)| Into::<&str>::into(todo_keyword)),
|
||||
title,
|
||||
tags: heading_tags,
|
||||
children,
|
||||
},
|
||||
))
|
||||
}
|
||||
|
||||
#[cfg_attr(feature = "tracing", tracing::instrument(ret, level = "debug"))]
|
||||
fn detect_headline<'s>(input: OrgSource<'s>) -> Res<OrgSource<'s>, ()> {
|
||||
tuple((start_of_line, many1(tag("*")), space1))(input)?;
|
||||
Ok((input, ()))
|
||||
}
|
||||
|
||||
#[cfg_attr(feature = "tracing", tracing::instrument(ret, level = "debug"))]
|
||||
fn headline<'r, 's>(
|
||||
context: Context<'r, 's>,
|
||||
input: OrgSource<'s>,
|
||||
parent_stars: usize,
|
||||
) -> Res<
|
||||
OrgSource<'s>,
|
||||
(
|
||||
usize,
|
||||
OrgSource<'s>,
|
||||
Option<(OrgSource<'s>, OrgSource<'s>)>,
|
||||
Vec<Object<'s>>,
|
||||
Vec<&'s str>,
|
||||
),
|
||||
> {
|
||||
let parser_context =
|
||||
context.with_additional_node(ContextElement::ExitMatcherNode(ExitMatcherNode {
|
||||
class: ExitClass::Document,
|
||||
exit_matcher: &headline_title_end,
|
||||
}));
|
||||
let standard_set_object_matcher = parser_with_context!(standard_set_object)(&parser_context);
|
||||
|
||||
let (
|
||||
remaining,
|
||||
(_sol, star_count, ws, maybe_todo_keyword, title, maybe_tags, _ws, _line_ending),
|
||||
) = tuple((
|
||||
start_of_line,
|
||||
verify(many1_count(tag("*")), |star_count| {
|
||||
*star_count > parent_stars
|
||||
}),
|
||||
space1,
|
||||
opt(tuple((heading_keyword, space1))),
|
||||
many1(standard_set_object_matcher),
|
||||
opt(tuple((space0, tags))),
|
||||
space0,
|
||||
alt((line_ending, eof)),
|
||||
))(input)?;
|
||||
Ok((
|
||||
remaining,
|
||||
(
|
||||
star_count,
|
||||
ws,
|
||||
maybe_todo_keyword,
|
||||
title,
|
||||
maybe_tags
|
||||
.map(|(_ws, tags)| {
|
||||
tags.into_iter()
|
||||
.map(|single_tag| Into::<&str>::into(single_tag))
|
||||
.collect()
|
||||
})
|
||||
.unwrap_or(Vec::new()),
|
||||
),
|
||||
))
|
||||
}
|
||||
|
||||
#[cfg_attr(feature = "tracing", tracing::instrument(ret, level = "debug"))]
|
||||
fn headline_title_end<'r, 's>(
|
||||
_context: Context<'r, 's>,
|
||||
input: OrgSource<'s>,
|
||||
) -> Res<OrgSource<'s>, OrgSource<'s>> {
|
||||
recognize(tuple((
|
||||
opt(tuple((space0, tags, space0))),
|
||||
alt((line_ending, eof)),
|
||||
)))(input)
|
||||
}
|
||||
|
||||
#[cfg_attr(feature = "tracing", tracing::instrument(ret, level = "debug"))]
|
||||
fn tags<'s>(input: OrgSource<'s>) -> Res<OrgSource<'s>, Vec<OrgSource<'s>>> {
|
||||
let (remaining, (_open, tags, _close)) =
|
||||
tuple((tag(":"), separated_list1(tag(":"), single_tag), tag(":")))(input)?;
|
||||
Ok((remaining, tags))
|
||||
}
|
||||
|
||||
#[cfg_attr(feature = "tracing", tracing::instrument(ret, level = "debug"))]
|
||||
fn single_tag<'r, 's>(input: OrgSource<'s>) -> Res<OrgSource<'s>, OrgSource<'s>> {
|
||||
recognize(many1(verify(anychar, |c| {
|
||||
c.is_alphanumeric() || "_@#%".contains(*c)
|
||||
})))(input)
|
||||
}
|
||||
|
||||
#[cfg_attr(feature = "tracing", tracing::instrument(ret, level = "debug"))]
|
||||
fn heading_keyword<'s>(input: OrgSource<'s>) -> Res<OrgSource<'s>, OrgSource<'s>> {
|
||||
// TODO: This should take into account the value of "#+TODO:" ref https://orgmode.org/manual/Per_002dfile-keywords.html and possibly the configurable variable org-todo-keywords ref https://orgmode.org/manual/Workflow-states.html. Case is significant.
|
||||
alt((tag("TODO"), tag("DONE")))(input)
|
||||
}
|
||||
|
||||
impl<'s> Document<'s> {
|
||||
pub fn iter_tokens<'r>(&'r self) -> impl Iterator<Item = Token<'r, 's>> {
|
||||
AllTokensIterator::new(Token::Document(self))
|
||||
|
||||
@@ -11,29 +11,29 @@ use nom::multi::many_till;
|
||||
use nom::sequence::tuple;
|
||||
|
||||
use super::org_source::OrgSource;
|
||||
use super::Context;
|
||||
use crate::context::parser_with_context;
|
||||
use crate::context::ContextElement;
|
||||
use crate::context::ExitClass;
|
||||
use crate::context::ExitMatcherNode;
|
||||
use crate::context::RefContext;
|
||||
use crate::error::CustomError;
|
||||
use crate::error::MyError;
|
||||
use crate::error::Res;
|
||||
use crate::parser::element_parser::element;
|
||||
use crate::parser::exiting::ExitClass;
|
||||
use crate::parser::parser_context::ContextElement;
|
||||
use crate::parser::parser_context::ExitMatcherNode;
|
||||
use crate::parser::parser_with_context::parser_with_context;
|
||||
use crate::parser::source::SetSource;
|
||||
use crate::parser::util::blank_line;
|
||||
use crate::parser::util::exit_matcher_parser;
|
||||
use crate::parser::util::get_consumed;
|
||||
use crate::parser::util::immediate_in_section;
|
||||
use crate::parser::util::start_of_line;
|
||||
use crate::parser::util::WORD_CONSTITUENT_CHARACTERS;
|
||||
use crate::parser::Drawer;
|
||||
use crate::parser::Element;
|
||||
use crate::parser::Paragraph;
|
||||
use crate::types::Drawer;
|
||||
use crate::types::Element;
|
||||
use crate::types::Paragraph;
|
||||
use crate::types::SetSource;
|
||||
|
||||
#[cfg_attr(feature = "tracing", tracing::instrument(ret, level = "debug"))]
|
||||
pub fn drawer<'r, 's>(
|
||||
context: Context<'r, 's>,
|
||||
pub fn drawer<'b, 'g, 'r, 's>(
|
||||
context: RefContext<'b, 'g, 'r, 's>,
|
||||
input: OrgSource<'s>,
|
||||
) -> Res<OrgSource<'s>, Drawer<'s>> {
|
||||
if immediate_in_section(context, "drawer") {
|
||||
@@ -50,13 +50,17 @@ pub fn drawer<'r, 's>(
|
||||
recognize(tuple((space0, line_ending))),
|
||||
))(remaining)?;
|
||||
|
||||
let parser_context = context
|
||||
.with_additional_node(ContextElement::ConsumeTrailingWhitespace(true))
|
||||
.with_additional_node(ContextElement::Context("drawer"))
|
||||
.with_additional_node(ContextElement::ExitMatcherNode(ExitMatcherNode {
|
||||
let contexts = [
|
||||
ContextElement::ConsumeTrailingWhitespace(true),
|
||||
ContextElement::Context("drawer"),
|
||||
ContextElement::ExitMatcherNode(ExitMatcherNode {
|
||||
class: ExitClass::Alpha,
|
||||
exit_matcher: &drawer_end,
|
||||
}));
|
||||
}),
|
||||
];
|
||||
let parser_context = context.with_additional_node(&contexts[0]);
|
||||
let parser_context = parser_context.with_additional_node(&contexts[1]);
|
||||
let parser_context = parser_context.with_additional_node(&contexts[2]);
|
||||
|
||||
let element_matcher = parser_with_context!(element(true))(&parser_context);
|
||||
let exit_matcher = parser_with_context!(exit_matcher_parser)(&parser_context);
|
||||
@@ -98,8 +102,8 @@ fn name<'s>(input: OrgSource<'s>) -> Res<OrgSource<'s>, OrgSource<'s>> {
|
||||
}
|
||||
|
||||
#[cfg_attr(feature = "tracing", tracing::instrument(ret, level = "debug"))]
|
||||
fn drawer_end<'r, 's>(
|
||||
_context: Context<'r, 's>,
|
||||
fn drawer_end<'b, 'g, 'r, 's>(
|
||||
_context: RefContext<'b, 'g, 'r, 's>,
|
||||
input: OrgSource<'s>,
|
||||
) -> Res<OrgSource<'s>, OrgSource<'s>> {
|
||||
start_of_line(input)?;
|
||||
|
||||
@@ -12,28 +12,28 @@ use nom::multi::many_till;
|
||||
use nom::sequence::tuple;
|
||||
|
||||
use super::org_source::OrgSource;
|
||||
use super::Context;
|
||||
use crate::context::parser_with_context;
|
||||
use crate::context::ContextElement;
|
||||
use crate::context::ExitClass;
|
||||
use crate::context::ExitMatcherNode;
|
||||
use crate::context::RefContext;
|
||||
use crate::error::CustomError;
|
||||
use crate::error::MyError;
|
||||
use crate::error::Res;
|
||||
use crate::parser::element_parser::element;
|
||||
use crate::parser::exiting::ExitClass;
|
||||
use crate::parser::greater_element::DynamicBlock;
|
||||
use crate::parser::lesser_element::Paragraph;
|
||||
use crate::parser::parser_context::ContextElement;
|
||||
use crate::parser::parser_context::ExitMatcherNode;
|
||||
use crate::parser::parser_with_context::parser_with_context;
|
||||
use crate::parser::source::SetSource;
|
||||
use crate::parser::util::blank_line;
|
||||
use crate::parser::util::exit_matcher_parser;
|
||||
use crate::parser::util::get_consumed;
|
||||
use crate::parser::util::immediate_in_section;
|
||||
use crate::parser::util::start_of_line;
|
||||
use crate::parser::Element;
|
||||
use crate::types::DynamicBlock;
|
||||
use crate::types::Element;
|
||||
use crate::types::Paragraph;
|
||||
use crate::types::SetSource;
|
||||
|
||||
#[cfg_attr(feature = "tracing", tracing::instrument(ret, level = "debug"))]
|
||||
pub fn dynamic_block<'r, 's>(
|
||||
context: Context<'r, 's>,
|
||||
pub fn dynamic_block<'b, 'g, 'r, 's>(
|
||||
context: RefContext<'b, 'g, 'r, 's>,
|
||||
input: OrgSource<'s>,
|
||||
) -> Res<OrgSource<'s>, DynamicBlock<'s>> {
|
||||
// TODO: Do I need to differentiate between different dynamic block types.
|
||||
@@ -50,13 +50,17 @@ pub fn dynamic_block<'r, 's>(
|
||||
opt(tuple((space1, parameters))),
|
||||
line_ending,
|
||||
))(remaining)?;
|
||||
let parser_context = context
|
||||
.with_additional_node(ContextElement::ConsumeTrailingWhitespace(true))
|
||||
.with_additional_node(ContextElement::Context("dynamic block"))
|
||||
.with_additional_node(ContextElement::ExitMatcherNode(ExitMatcherNode {
|
||||
let contexts = [
|
||||
ContextElement::ConsumeTrailingWhitespace(true),
|
||||
ContextElement::Context("dynamic block"),
|
||||
ContextElement::ExitMatcherNode(ExitMatcherNode {
|
||||
class: ExitClass::Alpha,
|
||||
exit_matcher: &dynamic_block_end,
|
||||
}));
|
||||
}),
|
||||
];
|
||||
let parser_context = context.with_additional_node(&contexts[0]);
|
||||
let parser_context = parser_context.with_additional_node(&contexts[1]);
|
||||
let parser_context = parser_context.with_additional_node(&contexts[2]);
|
||||
let parameters = match parameters {
|
||||
Some((_ws, parameters)) => Some(parameters),
|
||||
None => None,
|
||||
@@ -106,8 +110,8 @@ fn parameters<'s>(input: OrgSource<'s>) -> Res<OrgSource<'s>, OrgSource<'s>> {
|
||||
}
|
||||
|
||||
#[cfg_attr(feature = "tracing", tracing::instrument(ret, level = "debug"))]
|
||||
fn dynamic_block_end<'r, 's>(
|
||||
_context: Context<'r, 's>,
|
||||
fn dynamic_block_end<'b, 'g, 'r, 's>(
|
||||
_context: RefContext<'b, 'g, 'r, 's>,
|
||||
input: OrgSource<'s>,
|
||||
) -> Res<OrgSource<'s>, OrgSource<'s>> {
|
||||
start_of_line(input)?;
|
||||
|
||||
@@ -7,12 +7,12 @@ use super::comment::comment;
|
||||
use super::diary_sexp::diary_sexp;
|
||||
use super::drawer::drawer;
|
||||
use super::dynamic_block::dynamic_block;
|
||||
use super::element::Element;
|
||||
use super::fixed_width_area::fixed_width_area;
|
||||
use super::footnote_definition::footnote_definition;
|
||||
use super::greater_block::greater_block;
|
||||
use super::horizontal_rule::horizontal_rule;
|
||||
use super::keyword::affiliated_keyword;
|
||||
use super::keyword::babel_call_keyword;
|
||||
use super::keyword::keyword;
|
||||
use super::latex_environment::latex_environment;
|
||||
use super::lesser_block::comment_block;
|
||||
@@ -24,25 +24,29 @@ use super::org_source::OrgSource;
|
||||
use super::paragraph::paragraph;
|
||||
use super::plain_list::detect_plain_list;
|
||||
use super::plain_list::plain_list;
|
||||
use super::source::SetSource;
|
||||
use super::util::get_consumed;
|
||||
use super::util::maybe_consume_trailing_whitespace_if_not_exiting;
|
||||
use super::Context;
|
||||
use crate::context::parser_with_context;
|
||||
use crate::context::RefContext;
|
||||
use crate::error::CustomError;
|
||||
use crate::error::MyError;
|
||||
use crate::error::Res;
|
||||
use crate::parser::parser_with_context::parser_with_context;
|
||||
use crate::parser::table::org_mode_table;
|
||||
use crate::types::Element;
|
||||
use crate::types::SetSource;
|
||||
|
||||
pub const fn element(
|
||||
can_be_paragraph: bool,
|
||||
) -> impl for<'r, 's> Fn(Context<'r, 's>, OrgSource<'s>) -> Res<OrgSource<'s>, Element<'s>> {
|
||||
move |context: Context, input: OrgSource<'_>| _element(context, input, can_be_paragraph)
|
||||
) -> impl for<'b, 'g, 'r, 's> Fn(
|
||||
RefContext<'b, 'g, 'r, 's>,
|
||||
OrgSource<'s>,
|
||||
) -> Res<OrgSource<'s>, Element<'s>> {
|
||||
move |context, input: OrgSource<'_>| _element(context, input, can_be_paragraph)
|
||||
}
|
||||
|
||||
#[cfg_attr(feature = "tracing", tracing::instrument(ret, level = "debug"))]
|
||||
fn _element<'r, 's>(
|
||||
context: Context<'r, 's>,
|
||||
fn _element<'b, 'g, 'r, 's>(
|
||||
context: RefContext<'b, 'g, 'r, 's>,
|
||||
input: OrgSource<'s>,
|
||||
can_be_paragraph: bool,
|
||||
) -> Res<OrgSource<'s>, Element<'s>> {
|
||||
@@ -64,6 +68,7 @@ fn _element<'r, 's>(
|
||||
let horizontal_rule_matcher = parser_with_context!(horizontal_rule)(context);
|
||||
let keyword_matcher = parser_with_context!(keyword)(context);
|
||||
let affiliated_keyword_matcher = parser_with_context!(affiliated_keyword)(context);
|
||||
let babel_keyword_matcher = parser_with_context!(babel_call_keyword)(context);
|
||||
let paragraph_matcher = parser_with_context!(paragraph)(context);
|
||||
let latex_environment_matcher = parser_with_context!(latex_environment)(context);
|
||||
|
||||
@@ -87,6 +92,7 @@ fn _element<'r, 's>(
|
||||
map(fixed_width_area_matcher, Element::FixedWidthArea),
|
||||
map(horizontal_rule_matcher, Element::HorizontalRule),
|
||||
map(latex_environment_matcher, Element::LatexEnvironment),
|
||||
map(babel_keyword_matcher, Element::BabelCall),
|
||||
map(keyword_matcher, Element::Keyword),
|
||||
))(remaining)
|
||||
{
|
||||
@@ -118,13 +124,14 @@ fn _element<'r, 's>(
|
||||
|
||||
pub const fn detect_element(
|
||||
can_be_paragraph: bool,
|
||||
) -> impl for<'r, 's> Fn(Context<'r, 's>, OrgSource<'s>) -> Res<OrgSource<'s>, ()> {
|
||||
move |context: Context, input: OrgSource<'_>| _detect_element(context, input, can_be_paragraph)
|
||||
) -> impl for<'b, 'g, 'r, 's> Fn(RefContext<'b, 'g, 'r, 's>, OrgSource<'s>) -> Res<OrgSource<'s>, ()>
|
||||
{
|
||||
move |context, input: OrgSource<'_>| _detect_element(context, input, can_be_paragraph)
|
||||
}
|
||||
|
||||
#[cfg_attr(feature = "tracing", tracing::instrument(ret, level = "debug"))]
|
||||
fn _detect_element<'r, 's>(
|
||||
context: Context<'r, 's>,
|
||||
fn _detect_element<'b, 'g, 'r, 's>(
|
||||
context: RefContext<'b, 'g, 'r, 's>,
|
||||
input: OrgSource<'s>,
|
||||
can_be_paragraph: bool,
|
||||
) -> Res<OrgSource<'s>, ()> {
|
||||
|
||||
@@ -8,12 +8,12 @@ use nom::combinator::recognize;
|
||||
|
||||
use super::org_source::OrgSource;
|
||||
use super::util::maybe_consume_object_trailing_whitespace_if_not_exiting;
|
||||
use super::Context;
|
||||
use crate::context::RefContext;
|
||||
use crate::error::CustomError;
|
||||
use crate::error::MyError;
|
||||
use crate::error::Res;
|
||||
use crate::parser::object::Entity;
|
||||
use crate::parser::util::get_consumed;
|
||||
use crate::types::Entity;
|
||||
|
||||
// TODO: Make this a user-provided variable corresponding to elisp's org-entities
|
||||
const ORG_ENTITIES: [&'static str; 413] = [
|
||||
@@ -433,8 +433,8 @@ const ORG_ENTITIES: [&'static str; 413] = [
|
||||
];
|
||||
|
||||
#[cfg_attr(feature = "tracing", tracing::instrument(ret, level = "debug"))]
|
||||
pub fn entity<'r, 's>(
|
||||
context: Context<'r, 's>,
|
||||
pub fn entity<'b, 'g, 'r, 's>(
|
||||
context: RefContext<'b, 'g, 'r, 's>,
|
||||
input: OrgSource<'s>,
|
||||
) -> Res<OrgSource<'s>, Entity<'s>> {
|
||||
let (remaining, _) = tag("\\")(input)?;
|
||||
@@ -454,8 +454,8 @@ pub fn entity<'r, 's>(
|
||||
}
|
||||
|
||||
#[cfg_attr(feature = "tracing", tracing::instrument(ret, level = "debug"))]
|
||||
fn name<'r, 's>(
|
||||
_context: Context<'r, 's>,
|
||||
fn name<'b, 'g, 'r, 's>(
|
||||
_context: RefContext<'b, 'g, 'r, 's>,
|
||||
input: OrgSource<'s>,
|
||||
) -> Res<OrgSource<'s>, OrgSource<'s>> {
|
||||
// TODO: This should be defined by org-entities and optionally org-entities-user
|
||||
|
||||
@@ -1,20 +0,0 @@
|
||||
#[derive(Debug, Copy, Clone)]
|
||||
pub enum ExitClass {
|
||||
/// Headlines and sections.
|
||||
Document = 1,
|
||||
|
||||
/// Elements who take priority over beta elements when matching.
|
||||
Alpha = 20,
|
||||
|
||||
/// Elements who cede priority to alpha elements when matching.
|
||||
Beta = 300,
|
||||
|
||||
/// Elements who cede priority to alpha and beta elements when matching.
|
||||
Gamma = 4000,
|
||||
}
|
||||
|
||||
impl std::fmt::Display for ExitClass {
|
||||
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
|
||||
write!(f, "{:?}", self)
|
||||
}
|
||||
}
|
||||
@@ -9,28 +9,28 @@ use nom::sequence::tuple;
|
||||
|
||||
use super::org_source::OrgSource;
|
||||
use super::util::maybe_consume_object_trailing_whitespace_if_not_exiting;
|
||||
use super::Context;
|
||||
use crate::context::parser_with_context;
|
||||
use crate::context::ContextElement;
|
||||
use crate::context::ExitClass;
|
||||
use crate::context::ExitMatcherNode;
|
||||
use crate::context::RefContext;
|
||||
use crate::error::Res;
|
||||
use crate::parser::exiting::ExitClass;
|
||||
use crate::parser::parser_context::ContextElement;
|
||||
use crate::parser::parser_context::ExitMatcherNode;
|
||||
use crate::parser::parser_with_context::parser_with_context;
|
||||
use crate::parser::util::exit_matcher_parser;
|
||||
use crate::parser::util::get_consumed;
|
||||
use crate::parser::ExportSnippet;
|
||||
use crate::types::ExportSnippet;
|
||||
|
||||
#[cfg_attr(feature = "tracing", tracing::instrument(ret, level = "debug"))]
|
||||
pub fn export_snippet<'r, 's>(
|
||||
context: Context<'r, 's>,
|
||||
pub fn export_snippet<'b, 'g, 'r, 's>(
|
||||
context: RefContext<'b, 'g, 'r, 's>,
|
||||
input: OrgSource<'s>,
|
||||
) -> Res<OrgSource<'s>, ExportSnippet<'s>> {
|
||||
let (remaining, _) = tag("@@")(input)?;
|
||||
let (remaining, backend_name) = backend(context, remaining)?;
|
||||
let parser_context =
|
||||
context.with_additional_node(ContextElement::ExitMatcherNode(ExitMatcherNode {
|
||||
class: ExitClass::Gamma,
|
||||
exit_matcher: &export_snippet_end,
|
||||
}));
|
||||
let parser_context = ContextElement::ExitMatcherNode(ExitMatcherNode {
|
||||
class: ExitClass::Gamma,
|
||||
exit_matcher: &export_snippet_end,
|
||||
});
|
||||
let parser_context = context.with_additional_node(&parser_context);
|
||||
let (remaining, backend_contents) = opt(tuple((
|
||||
tag(":"),
|
||||
parser_with_context!(contents)(&parser_context),
|
||||
@@ -50,8 +50,8 @@ pub fn export_snippet<'r, 's>(
|
||||
}
|
||||
|
||||
#[cfg_attr(feature = "tracing", tracing::instrument(ret, level = "debug"))]
|
||||
fn backend<'r, 's>(
|
||||
_context: Context<'r, 's>,
|
||||
fn backend<'b, 'g, 'r, 's>(
|
||||
_context: RefContext<'b, 'g, 'r, 's>,
|
||||
input: OrgSource<'s>,
|
||||
) -> Res<OrgSource<'s>, OrgSource<'s>> {
|
||||
let (remaining, backend_name) =
|
||||
@@ -61,8 +61,8 @@ fn backend<'r, 's>(
|
||||
}
|
||||
|
||||
#[cfg_attr(feature = "tracing", tracing::instrument(ret, level = "debug"))]
|
||||
fn contents<'r, 's>(
|
||||
context: Context<'r, 's>,
|
||||
fn contents<'b, 'g, 'r, 's>(
|
||||
context: RefContext<'b, 'g, 'r, 's>,
|
||||
input: OrgSource<'s>,
|
||||
) -> Res<OrgSource<'s>, OrgSource<'s>> {
|
||||
let (remaining, source) = recognize(verify(
|
||||
@@ -73,8 +73,8 @@ fn contents<'r, 's>(
|
||||
}
|
||||
|
||||
#[cfg_attr(feature = "tracing", tracing::instrument(ret, level = "debug"))]
|
||||
fn export_snippet_end<'r, 's>(
|
||||
_context: Context<'r, 's>,
|
||||
fn export_snippet_end<'b, 'g, 'r, 's>(
|
||||
_context: RefContext<'b, 'g, 'r, 's>,
|
||||
input: OrgSource<'s>,
|
||||
) -> Res<OrgSource<'s>, OrgSource<'s>> {
|
||||
tag("@@")(input)
|
||||
|
||||
@@ -12,17 +12,17 @@ use nom::sequence::preceded;
|
||||
use nom::sequence::tuple;
|
||||
|
||||
use super::org_source::OrgSource;
|
||||
use super::Context;
|
||||
use crate::context::parser_with_context;
|
||||
use crate::context::RefContext;
|
||||
use crate::error::Res;
|
||||
use crate::parser::parser_with_context::parser_with_context;
|
||||
use crate::parser::util::exit_matcher_parser;
|
||||
use crate::parser::util::get_consumed;
|
||||
use crate::parser::util::start_of_line;
|
||||
use crate::parser::FixedWidthArea;
|
||||
use crate::types::FixedWidthArea;
|
||||
|
||||
#[cfg_attr(feature = "tracing", tracing::instrument(ret, level = "debug"))]
|
||||
pub fn fixed_width_area<'r, 's>(
|
||||
context: Context<'r, 's>,
|
||||
pub fn fixed_width_area<'b, 'g, 'r, 's>(
|
||||
context: RefContext<'b, 'g, 'r, 's>,
|
||||
input: OrgSource<'s>,
|
||||
) -> Res<OrgSource<'s>, FixedWidthArea<'s>> {
|
||||
let fixed_width_area_line_matcher = parser_with_context!(fixed_width_area_line)(context);
|
||||
@@ -41,8 +41,8 @@ pub fn fixed_width_area<'r, 's>(
|
||||
}
|
||||
|
||||
#[cfg_attr(feature = "tracing", tracing::instrument(ret, level = "debug"))]
|
||||
fn fixed_width_area_line<'r, 's>(
|
||||
_context: Context<'r, 's>,
|
||||
fn fixed_width_area_line<'b, 'g, 'r, 's>(
|
||||
_context: RefContext<'b, 'g, 'r, 's>,
|
||||
input: OrgSource<'s>,
|
||||
) -> Res<OrgSource<'s>, OrgSource<'s>> {
|
||||
start_of_line(input)?;
|
||||
|
||||
@@ -4,34 +4,37 @@ use nom::bytes::complete::tag_no_case;
|
||||
use nom::bytes::complete::take_while;
|
||||
use nom::character::complete::digit1;
|
||||
use nom::character::complete::space0;
|
||||
use nom::combinator::opt;
|
||||
use nom::combinator::recognize;
|
||||
use nom::combinator::verify;
|
||||
use nom::multi::many0;
|
||||
use nom::multi::many1;
|
||||
use nom::multi::many_till;
|
||||
use nom::sequence::tuple;
|
||||
|
||||
use super::org_source::OrgSource;
|
||||
use super::util::include_input;
|
||||
use super::util::WORD_CONSTITUENT_CHARACTERS;
|
||||
use super::Context;
|
||||
use crate::context::parser_with_context;
|
||||
use crate::context::ContextElement;
|
||||
use crate::context::ExitClass;
|
||||
use crate::context::ExitMatcherNode;
|
||||
use crate::context::RefContext;
|
||||
use crate::error::CustomError;
|
||||
use crate::error::MyError;
|
||||
use crate::error::Res;
|
||||
use crate::parser::element_parser::element;
|
||||
use crate::parser::exiting::ExitClass;
|
||||
use crate::parser::greater_element::FootnoteDefinition;
|
||||
use crate::parser::parser_context::ContextElement;
|
||||
use crate::parser::parser_context::ExitMatcherNode;
|
||||
use crate::parser::parser_with_context::parser_with_context;
|
||||
use crate::parser::util::blank_line;
|
||||
use crate::parser::util::exit_matcher_parser;
|
||||
use crate::parser::util::get_consumed;
|
||||
use crate::parser::util::immediate_in_section;
|
||||
use crate::parser::util::maybe_consume_trailing_whitespace;
|
||||
use crate::parser::util::start_of_line;
|
||||
use crate::types::FootnoteDefinition;
|
||||
|
||||
#[cfg_attr(feature = "tracing", tracing::instrument(ret, level = "debug"))]
|
||||
pub fn footnote_definition<'r, 's>(
|
||||
context: Context<'r, 's>,
|
||||
pub fn footnote_definition<'b, 'g, 'r, 's>(
|
||||
context: RefContext<'b, 'g, 'r, 's>,
|
||||
input: OrgSource<'s>,
|
||||
) -> Res<OrgSource<'s>, FootnoteDefinition<'s>> {
|
||||
if immediate_in_section(context, "footnote definition") {
|
||||
@@ -41,20 +44,42 @@ pub fn footnote_definition<'r, 's>(
|
||||
}
|
||||
start_of_line(input)?;
|
||||
// Cannot be indented.
|
||||
let (remaining, (_lead_in, lbl, _lead_out, _ws)) =
|
||||
tuple((tag_no_case("[fn:"), label, tag("]"), space0))(input)?;
|
||||
let parser_context = context
|
||||
.with_additional_node(ContextElement::ConsumeTrailingWhitespace(true))
|
||||
.with_additional_node(ContextElement::Context("footnote definition"))
|
||||
.with_additional_node(ContextElement::ExitMatcherNode(ExitMatcherNode {
|
||||
let (remaining, (_, lbl, _, _, _)) = tuple((
|
||||
tag_no_case("[fn:"),
|
||||
label,
|
||||
tag("]"),
|
||||
space0,
|
||||
opt(verify(many0(blank_line), |lines: &Vec<OrgSource<'_>>| {
|
||||
lines.len() <= 2
|
||||
})),
|
||||
))(input)?;
|
||||
let contexts = [
|
||||
ContextElement::ConsumeTrailingWhitespace(true),
|
||||
ContextElement::Context("footnote definition"),
|
||||
ContextElement::ExitMatcherNode(ExitMatcherNode {
|
||||
class: ExitClass::Alpha,
|
||||
exit_matcher: &footnote_definition_end,
|
||||
}));
|
||||
// TODO: The problem is we are not accounting for trailing whitespace like we do in section. Maybe it would be easier if we passed down whether or not to parse trailing whitespace into the element matcher similar to how tag takes in parameters.
|
||||
}),
|
||||
];
|
||||
let parser_context = context.with_additional_node(&contexts[0]);
|
||||
let parser_context = parser_context.with_additional_node(&contexts[1]);
|
||||
let parser_context = parser_context.with_additional_node(&contexts[2]);
|
||||
let element_matcher = parser_with_context!(element(true))(&parser_context);
|
||||
let exit_matcher = parser_with_context!(exit_matcher_parser)(&parser_context);
|
||||
let (remaining, (children, _exit_contents)) =
|
||||
many_till(element_matcher, exit_matcher)(remaining)?;
|
||||
let (mut remaining, (mut children, _exit_contents)) =
|
||||
many_till(include_input(element_matcher), exit_matcher)(remaining)?;
|
||||
|
||||
// Re-parse the last element of the footnote definition with consume trailing whitespace off because the trailing whitespace needs to belong to the footnote definition, not the contents.
|
||||
if context.should_consume_trailing_whitespace() {
|
||||
if let Some((final_item_input, _)) = children.pop() {
|
||||
let final_item_context = ContextElement::ConsumeTrailingWhitespace(false);
|
||||
let final_item_context = parser_context.with_additional_node(&final_item_context);
|
||||
let (remain, reparsed_final_item) =
|
||||
parser_with_context!(element(true))(&final_item_context)(final_item_input)?;
|
||||
children.push((final_item_input, reparsed_final_item));
|
||||
remaining = remain;
|
||||
}
|
||||
}
|
||||
|
||||
let source = get_consumed(input, remaining);
|
||||
Ok((
|
||||
@@ -62,7 +87,7 @@ pub fn footnote_definition<'r, 's>(
|
||||
FootnoteDefinition {
|
||||
source: source.into(),
|
||||
label: lbl.into(),
|
||||
children,
|
||||
children: children.into_iter().map(|(_, item)| item).collect(),
|
||||
},
|
||||
))
|
||||
}
|
||||
@@ -76,8 +101,8 @@ pub fn label<'s>(input: OrgSource<'s>) -> Res<OrgSource<'s>, OrgSource<'s>> {
|
||||
}
|
||||
|
||||
#[cfg_attr(feature = "tracing", tracing::instrument(ret, level = "debug"))]
|
||||
fn footnote_definition_end<'r, 's>(
|
||||
context: Context<'r, 's>,
|
||||
fn footnote_definition_end<'b, 'g, 'r, 's>(
|
||||
context: RefContext<'b, 'g, 'r, 's>,
|
||||
input: OrgSource<'s>,
|
||||
) -> Res<OrgSource<'s>, OrgSource<'s>> {
|
||||
let (remaining, source) = alt((
|
||||
@@ -105,9 +130,10 @@ fn detect_footnote_definition<'s>(input: OrgSource<'s>) -> Res<OrgSource<'s>, ()
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
use crate::parser::parser_context::ContextTree;
|
||||
use crate::parser::parser_with_context::parser_with_context;
|
||||
use crate::parser::Source;
|
||||
use crate::context::Context;
|
||||
use crate::context::GlobalSettings;
|
||||
use crate::context::List;
|
||||
use crate::types::Source;
|
||||
|
||||
#[test]
|
||||
fn two_paragraphs() {
|
||||
@@ -118,7 +144,9 @@ mod tests {
|
||||
|
||||
line footnote.",
|
||||
);
|
||||
let initial_context: ContextTree<'_, '_> = ContextTree::new();
|
||||
let global_settings = GlobalSettings::default();
|
||||
let initial_context = ContextElement::document_context();
|
||||
let initial_context = Context::new(&global_settings, List::new(&initial_context));
|
||||
let footnote_definition_matcher = parser_with_context!(element(true))(&initial_context);
|
||||
let (remaining, first_footnote_definition) =
|
||||
footnote_definition_matcher(input).expect("Parse first footnote_definition");
|
||||
@@ -149,7 +177,9 @@ line footnote.
|
||||
|
||||
not in the footnote.",
|
||||
);
|
||||
let initial_context: ContextTree<'_, '_> = ContextTree::new();
|
||||
let global_settings = GlobalSettings::default();
|
||||
let initial_context = ContextElement::document_context();
|
||||
let initial_context = Context::new(&global_settings, List::new(&initial_context));
|
||||
let footnote_definition_matcher = parser_with_context!(element(true))(&initial_context);
|
||||
let (remaining, first_footnote_definition) =
|
||||
footnote_definition_matcher(input).expect("Parse first footnote_definition");
|
||||
|
||||
@@ -6,24 +6,25 @@ use nom::multi::many_till;
|
||||
|
||||
use super::org_source::BracketDepth;
|
||||
use super::org_source::OrgSource;
|
||||
use super::parser_context::ContextElement;
|
||||
use super::util::maybe_consume_object_trailing_whitespace_if_not_exiting;
|
||||
use super::Context;
|
||||
use crate::context::parser_with_context;
|
||||
use crate::context::ContextElement;
|
||||
use crate::context::ContextMatcher;
|
||||
use crate::context::ExitClass;
|
||||
use crate::context::ExitMatcherNode;
|
||||
use crate::context::RefContext;
|
||||
use crate::error::CustomError;
|
||||
use crate::error::MyError;
|
||||
use crate::error::Res;
|
||||
use crate::parser::exiting::ExitClass;
|
||||
use crate::parser::footnote_definition::label;
|
||||
use crate::parser::object_parser::standard_set_object;
|
||||
use crate::parser::parser_context::ExitMatcherNode;
|
||||
use crate::parser::parser_with_context::parser_with_context;
|
||||
use crate::parser::util::exit_matcher_parser;
|
||||
use crate::parser::util::get_consumed;
|
||||
use crate::parser::FootnoteReference;
|
||||
use crate::types::FootnoteReference;
|
||||
|
||||
#[cfg_attr(feature = "tracing", tracing::instrument(ret, level = "debug"))]
|
||||
pub fn footnote_reference<'r, 's>(
|
||||
context: Context<'r, 's>,
|
||||
pub fn footnote_reference<'b, 'g, 'r, 's>(
|
||||
context: RefContext<'b, 'g, 'r, 's>,
|
||||
input: OrgSource<'s>,
|
||||
) -> Res<OrgSource<'s>, FootnoteReference<'s>> {
|
||||
alt((
|
||||
@@ -34,17 +35,17 @@ pub fn footnote_reference<'r, 's>(
|
||||
}
|
||||
|
||||
#[cfg_attr(feature = "tracing", tracing::instrument(ret, level = "debug"))]
|
||||
fn anonymous_footnote<'r, 's>(
|
||||
context: Context<'r, 's>,
|
||||
fn anonymous_footnote<'b, 'g, 'r, 's>(
|
||||
context: RefContext<'b, 'g, 'r, 's>,
|
||||
input: OrgSource<'s>,
|
||||
) -> Res<OrgSource<'s>, FootnoteReference<'s>> {
|
||||
let (remaining, _) = tag_no_case("[fn::")(input)?;
|
||||
let exit_with_depth = footnote_definition_end(remaining.get_bracket_depth());
|
||||
let parser_context =
|
||||
context.with_additional_node(ContextElement::ExitMatcherNode(ExitMatcherNode {
|
||||
class: ExitClass::Beta,
|
||||
exit_matcher: &exit_with_depth,
|
||||
}));
|
||||
let parser_context = ContextElement::ExitMatcherNode(ExitMatcherNode {
|
||||
class: ExitClass::Gamma,
|
||||
exit_matcher: &exit_with_depth,
|
||||
});
|
||||
let parser_context = context.with_additional_node(&parser_context);
|
||||
let (remaining, (children, _exit_contents)) = verify(
|
||||
many_till(
|
||||
parser_with_context!(standard_set_object)(&parser_context),
|
||||
@@ -68,19 +69,19 @@ fn anonymous_footnote<'r, 's>(
|
||||
}
|
||||
|
||||
#[cfg_attr(feature = "tracing", tracing::instrument(ret, level = "debug"))]
|
||||
fn inline_footnote<'r, 's>(
|
||||
context: Context<'r, 's>,
|
||||
fn inline_footnote<'b, 'g, 'r, 's>(
|
||||
context: RefContext<'b, 'g, 'r, 's>,
|
||||
input: OrgSource<'s>,
|
||||
) -> Res<OrgSource<'s>, FootnoteReference<'s>> {
|
||||
let (remaining, _) = tag_no_case("[fn:")(input)?;
|
||||
let (remaining, label_contents) = label(remaining)?;
|
||||
let (remaining, _) = tag(":")(remaining)?;
|
||||
let exit_with_depth = footnote_definition_end(remaining.get_bracket_depth());
|
||||
let parser_context =
|
||||
context.with_additional_node(ContextElement::ExitMatcherNode(ExitMatcherNode {
|
||||
class: ExitClass::Beta,
|
||||
exit_matcher: &exit_with_depth,
|
||||
}));
|
||||
let parser_context = ContextElement::ExitMatcherNode(ExitMatcherNode {
|
||||
class: ExitClass::Gamma,
|
||||
exit_matcher: &exit_with_depth,
|
||||
});
|
||||
let parser_context = context.with_additional_node(&parser_context);
|
||||
let (remaining, (children, _exit_contents)) = verify(
|
||||
many_till(
|
||||
parser_with_context!(standard_set_object)(&parser_context),
|
||||
@@ -104,8 +105,8 @@ fn inline_footnote<'r, 's>(
|
||||
}
|
||||
|
||||
#[cfg_attr(feature = "tracing", tracing::instrument(ret, level = "debug"))]
|
||||
fn footnote_reference_only<'r, 's>(
|
||||
context: Context<'r, 's>,
|
||||
fn footnote_reference_only<'b, 'g, 'r, 's>(
|
||||
context: RefContext<'b, 'g, 'r, 's>,
|
||||
input: OrgSource<'s>,
|
||||
) -> Res<OrgSource<'s>, FootnoteReference<'s>> {
|
||||
let (remaining, _) = tag_no_case("[fn:")(input)?;
|
||||
@@ -124,17 +125,15 @@ fn footnote_reference_only<'r, 's>(
|
||||
))
|
||||
}
|
||||
|
||||
fn footnote_definition_end(
|
||||
starting_bracket_depth: BracketDepth,
|
||||
) -> impl for<'r, 's> Fn(Context<'r, 's>, OrgSource<'s>) -> Res<OrgSource<'s>, OrgSource<'s>> {
|
||||
move |context: Context, input: OrgSource<'_>| {
|
||||
fn footnote_definition_end(starting_bracket_depth: BracketDepth) -> impl ContextMatcher {
|
||||
move |context, input: OrgSource<'_>| {
|
||||
_footnote_definition_end(context, input, starting_bracket_depth)
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg_attr(feature = "tracing", tracing::instrument(ret, level = "debug"))]
|
||||
fn _footnote_definition_end<'r, 's>(
|
||||
_context: Context<'r, 's>,
|
||||
fn _footnote_definition_end<'b, 'g, 'r, 's>(
|
||||
_context: RefContext<'b, 'g, 'r, 's>,
|
||||
input: OrgSource<'s>,
|
||||
starting_bracket_depth: BracketDepth,
|
||||
) -> Res<OrgSource<'s>, OrgSource<'s>> {
|
||||
|
||||
@@ -13,27 +13,28 @@ use nom::sequence::tuple;
|
||||
|
||||
use super::org_source::OrgSource;
|
||||
use super::util::in_section;
|
||||
use super::Context;
|
||||
use crate::context::parser_with_context;
|
||||
use crate::context::ContextElement;
|
||||
use crate::context::ContextMatcher;
|
||||
use crate::context::ExitClass;
|
||||
use crate::context::ExitMatcherNode;
|
||||
use crate::context::RefContext;
|
||||
use crate::error::CustomError;
|
||||
use crate::error::MyError;
|
||||
use crate::error::Res;
|
||||
use crate::parser::element_parser::element;
|
||||
use crate::parser::exiting::ExitClass;
|
||||
use crate::parser::greater_element::GreaterBlock;
|
||||
use crate::parser::parser_context::ContextElement;
|
||||
use crate::parser::parser_context::ExitMatcherNode;
|
||||
use crate::parser::parser_with_context::parser_with_context;
|
||||
use crate::parser::source::SetSource;
|
||||
use crate::parser::util::blank_line;
|
||||
use crate::parser::util::exit_matcher_parser;
|
||||
use crate::parser::util::get_consumed;
|
||||
use crate::parser::util::start_of_line;
|
||||
use crate::parser::Element;
|
||||
use crate::parser::Paragraph;
|
||||
use crate::types::Element;
|
||||
use crate::types::GreaterBlock;
|
||||
use crate::types::Paragraph;
|
||||
use crate::types::SetSource;
|
||||
|
||||
#[cfg_attr(feature = "tracing", tracing::instrument(ret, level = "debug"))]
|
||||
pub fn greater_block<'r, 's>(
|
||||
context: Context<'r, 's>,
|
||||
pub fn greater_block<'b, 'g, 'r, 's>(
|
||||
context: RefContext<'b, 'g, 'r, 's>,
|
||||
input: OrgSource<'s>,
|
||||
) -> Res<OrgSource<'s>, GreaterBlock<'s>> {
|
||||
// TODO: Do I need to differentiate between different greater block types.
|
||||
@@ -61,13 +62,17 @@ pub fn greater_block<'r, 's>(
|
||||
let exit_with_name = greater_block_end(name.into());
|
||||
let (remaining, parameters) = opt(tuple((space1, parameters)))(remaining)?;
|
||||
let (remaining, _nl) = line_ending(remaining)?;
|
||||
let parser_context = context
|
||||
.with_additional_node(ContextElement::ConsumeTrailingWhitespace(true))
|
||||
.with_additional_node(ContextElement::Context(context_name.as_str()))
|
||||
.with_additional_node(ContextElement::ExitMatcherNode(ExitMatcherNode {
|
||||
let contexts = [
|
||||
ContextElement::ConsumeTrailingWhitespace(true),
|
||||
ContextElement::Context(context_name.as_str()),
|
||||
ContextElement::ExitMatcherNode(ExitMatcherNode {
|
||||
class: ExitClass::Alpha,
|
||||
exit_matcher: &exit_with_name,
|
||||
}));
|
||||
}),
|
||||
];
|
||||
let parser_context = context.with_additional_node(&contexts[0]);
|
||||
let parser_context = parser_context.with_additional_node(&contexts[1]);
|
||||
let parser_context = parser_context.with_additional_node(&contexts[2]);
|
||||
let parameters = match parameters {
|
||||
Some((_ws, parameters)) => Some(parameters),
|
||||
None => None,
|
||||
@@ -120,19 +125,16 @@ fn parameters<'s>(input: OrgSource<'s>) -> Res<OrgSource<'s>, OrgSource<'s>> {
|
||||
is_not("\r\n")(input)
|
||||
}
|
||||
|
||||
fn greater_block_end<'x>(
|
||||
name: &'x str,
|
||||
) -> impl for<'r, 's> Fn(Context<'r, 's>, OrgSource<'s>) -> Res<OrgSource<'s>, OrgSource<'s>> {
|
||||
fn greater_block_end<'c>(name: &'c str) -> impl ContextMatcher + 'c {
|
||||
// TODO: Can this be done without making an owned copy?
|
||||
let name = name.to_owned();
|
||||
move |context: Context, input: OrgSource<'_>| _greater_block_end(context, input, name.as_str())
|
||||
move |context, input: OrgSource<'_>| _greater_block_end(context, input, name)
|
||||
}
|
||||
|
||||
#[cfg_attr(feature = "tracing", tracing::instrument(ret, level = "debug"))]
|
||||
fn _greater_block_end<'r, 's, 'x>(
|
||||
_context: Context<'r, 's>,
|
||||
fn _greater_block_end<'b, 'g, 'r, 's, 'c>(
|
||||
_context: RefContext<'b, 'g, 'r, 's>,
|
||||
input: OrgSource<'s>,
|
||||
name: &'x str,
|
||||
name: &'c str,
|
||||
) -> Res<OrgSource<'s>, OrgSource<'s>> {
|
||||
start_of_line(input)?;
|
||||
let (remaining, _leading_whitespace) = space0(input)?;
|
||||
|
||||
258
src/parser/headline.rs
Normal file
258
src/parser/headline.rs
Normal file
@@ -0,0 +1,258 @@
|
||||
use nom::branch::alt;
|
||||
use nom::bytes::complete::tag;
|
||||
use nom::character::complete::anychar;
|
||||
use nom::character::complete::line_ending;
|
||||
use nom::character::complete::space0;
|
||||
use nom::character::complete::space1;
|
||||
use nom::combinator::eof;
|
||||
use nom::combinator::map;
|
||||
use nom::combinator::not;
|
||||
use nom::combinator::opt;
|
||||
use nom::combinator::recognize;
|
||||
use nom::combinator::verify;
|
||||
use nom::multi::many0;
|
||||
use nom::multi::many1;
|
||||
use nom::multi::many1_count;
|
||||
use nom::multi::separated_list1;
|
||||
use nom::sequence::tuple;
|
||||
|
||||
use super::org_source::OrgSource;
|
||||
use super::section::section;
|
||||
use super::util::get_consumed;
|
||||
use super::util::start_of_line;
|
||||
use crate::context::parser_with_context;
|
||||
use crate::context::ContextElement;
|
||||
use crate::context::ExitClass;
|
||||
use crate::context::ExitMatcherNode;
|
||||
use crate::context::RefContext;
|
||||
use crate::error::CustomError;
|
||||
use crate::error::MyError;
|
||||
use crate::error::Res;
|
||||
use crate::parser::object_parser::standard_set_object;
|
||||
use crate::parser::util::blank_line;
|
||||
use crate::types::DocumentElement;
|
||||
use crate::types::Heading;
|
||||
use crate::types::Object;
|
||||
use crate::types::PriorityCookie;
|
||||
use crate::types::TodoKeywordType;
|
||||
|
||||
pub const fn heading(
|
||||
parent_stars: usize,
|
||||
) -> impl for<'b, 'g, 'r, 's> Fn(
|
||||
RefContext<'b, 'g, 'r, 's>,
|
||||
OrgSource<'s>,
|
||||
) -> Res<OrgSource<'s>, Heading<'s>> {
|
||||
move |context, input: OrgSource<'_>| _heading(context, input, parent_stars)
|
||||
}
|
||||
|
||||
#[cfg_attr(feature = "tracing", tracing::instrument(ret, level = "debug"))]
|
||||
fn _heading<'b, 'g, 'r, 's>(
|
||||
context: RefContext<'b, 'g, 'r, 's>,
|
||||
input: OrgSource<'s>,
|
||||
parent_stars: usize,
|
||||
) -> Res<OrgSource<'s>, Heading<'s>> {
|
||||
not(|i| context.check_exit_matcher(i))(input)?;
|
||||
let (
|
||||
remaining,
|
||||
(star_count, maybe_todo_keyword, maybe_priority, maybe_comment, title, heading_tags),
|
||||
) = headline(context, input, parent_stars)?;
|
||||
let section_matcher = parser_with_context!(section)(context);
|
||||
let heading_matcher = parser_with_context!(heading(star_count))(context);
|
||||
let (remaining, maybe_section) =
|
||||
opt(map(section_matcher, DocumentElement::Section))(remaining)?;
|
||||
let (remaining, _ws) = opt(tuple((start_of_line, many0(blank_line))))(remaining)?;
|
||||
let (remaining, mut children) =
|
||||
many0(map(heading_matcher, DocumentElement::Heading))(remaining)?;
|
||||
if let Some(section) = maybe_section {
|
||||
children.insert(0, section);
|
||||
}
|
||||
let remaining = if children.is_empty() {
|
||||
// Support empty headings
|
||||
let (remain, _ws) = many0(blank_line)(remaining)?;
|
||||
remain
|
||||
} else {
|
||||
remaining
|
||||
};
|
||||
let is_archived = heading_tags.contains(&"ARCHIVE");
|
||||
|
||||
let source = get_consumed(input, remaining);
|
||||
Ok((
|
||||
remaining,
|
||||
Heading {
|
||||
source: source.into(),
|
||||
stars: star_count,
|
||||
todo_keyword: maybe_todo_keyword.map(|((todo_keyword_type, todo_keyword), _ws)| {
|
||||
(todo_keyword_type, Into::<&str>::into(todo_keyword))
|
||||
}),
|
||||
priority_cookie: maybe_priority.map(|(priority, _)| priority),
|
||||
title,
|
||||
tags: heading_tags,
|
||||
children,
|
||||
is_comment: maybe_comment.is_some(),
|
||||
is_archived,
|
||||
},
|
||||
))
|
||||
}
|
||||
|
||||
#[cfg_attr(feature = "tracing", tracing::instrument(ret, level = "debug"))]
|
||||
pub fn detect_headline<'s>(input: OrgSource<'s>) -> Res<OrgSource<'s>, ()> {
|
||||
tuple((start_of_line, many1(tag("*")), space1))(input)?;
|
||||
Ok((input, ()))
|
||||
}
|
||||
|
||||
#[cfg_attr(feature = "tracing", tracing::instrument(ret, level = "debug"))]
|
||||
fn headline<'b, 'g, 'r, 's>(
|
||||
context: RefContext<'b, 'g, 'r, 's>,
|
||||
input: OrgSource<'s>,
|
||||
parent_stars: usize,
|
||||
) -> Res<
|
||||
OrgSource<'s>,
|
||||
(
|
||||
usize,
|
||||
Option<((TodoKeywordType, OrgSource<'s>), OrgSource<'s>)>,
|
||||
Option<(PriorityCookie, OrgSource<'s>)>,
|
||||
Option<(OrgSource<'s>, OrgSource<'s>)>,
|
||||
Vec<Object<'s>>,
|
||||
Vec<&'s str>,
|
||||
),
|
||||
> {
|
||||
let parser_context = ContextElement::ExitMatcherNode(ExitMatcherNode {
|
||||
class: ExitClass::Document,
|
||||
exit_matcher: &headline_title_end,
|
||||
});
|
||||
let parser_context = context.with_additional_node(&parser_context);
|
||||
|
||||
let (
|
||||
remaining,
|
||||
(
|
||||
_,
|
||||
star_count,
|
||||
_,
|
||||
maybe_todo_keyword,
|
||||
maybe_priority,
|
||||
maybe_comment,
|
||||
title,
|
||||
maybe_tags,
|
||||
_,
|
||||
_,
|
||||
),
|
||||
) = tuple((
|
||||
start_of_line,
|
||||
verify(many1_count(tag("*")), |star_count| {
|
||||
*star_count > parent_stars
|
||||
}),
|
||||
space1,
|
||||
opt(tuple((
|
||||
parser_with_context!(heading_keyword)(&parser_context),
|
||||
space1,
|
||||
))),
|
||||
opt(tuple((priority_cookie, space1))),
|
||||
opt(tuple((tag("COMMENT"), space1))),
|
||||
many1(parser_with_context!(standard_set_object)(&parser_context)),
|
||||
opt(tuple((space0, tags))),
|
||||
space0,
|
||||
alt((line_ending, eof)),
|
||||
))(input)?;
|
||||
Ok((
|
||||
remaining,
|
||||
(
|
||||
star_count,
|
||||
maybe_todo_keyword,
|
||||
maybe_priority,
|
||||
maybe_comment,
|
||||
title,
|
||||
maybe_tags
|
||||
.map(|(_ws, tags)| {
|
||||
tags.into_iter()
|
||||
.map(|single_tag| Into::<&str>::into(single_tag))
|
||||
.collect()
|
||||
})
|
||||
.unwrap_or(Vec::new()),
|
||||
),
|
||||
))
|
||||
}
|
||||
|
||||
#[cfg_attr(feature = "tracing", tracing::instrument(ret, level = "debug"))]
|
||||
fn headline_title_end<'b, 'g, 'r, 's>(
|
||||
_context: RefContext<'b, 'g, 'r, 's>,
|
||||
input: OrgSource<'s>,
|
||||
) -> Res<OrgSource<'s>, OrgSource<'s>> {
|
||||
recognize(tuple((
|
||||
opt(tuple((space0, tags, space0))),
|
||||
alt((line_ending, eof)),
|
||||
)))(input)
|
||||
}
|
||||
|
||||
#[cfg_attr(feature = "tracing", tracing::instrument(ret, level = "debug"))]
|
||||
fn tags<'s>(input: OrgSource<'s>) -> Res<OrgSource<'s>, Vec<OrgSource<'s>>> {
|
||||
let (remaining, (_open, tags, _close)) =
|
||||
tuple((tag(":"), separated_list1(tag(":"), single_tag), tag(":")))(input)?;
|
||||
Ok((remaining, tags))
|
||||
}
|
||||
|
||||
#[cfg_attr(feature = "tracing", tracing::instrument(ret, level = "debug"))]
|
||||
fn single_tag<'r, 's>(input: OrgSource<'s>) -> Res<OrgSource<'s>, OrgSource<'s>> {
|
||||
recognize(many1(verify(anychar, |c| {
|
||||
c.is_alphanumeric() || "_@#%".contains(*c)
|
||||
})))(input)
|
||||
}
|
||||
|
||||
#[cfg_attr(feature = "tracing", tracing::instrument(ret, level = "debug"))]
|
||||
fn heading_keyword<'b, 'g, 'r, 's>(
|
||||
context: RefContext<'b, 'g, 'r, 's>,
|
||||
input: OrgSource<'s>,
|
||||
) -> Res<OrgSource<'s>, (TodoKeywordType, OrgSource<'s>)> {
|
||||
let global_settings = context.get_global_settings();
|
||||
if global_settings.in_progress_todo_keywords.is_empty()
|
||||
&& global_settings.complete_todo_keywords.is_empty()
|
||||
{
|
||||
alt((
|
||||
map(tag("TODO"), |capture| (TodoKeywordType::Todo, capture)),
|
||||
map(tag("DONE"), |capture| (TodoKeywordType::Done, capture)),
|
||||
))(input)
|
||||
} else {
|
||||
for todo_keyword in global_settings
|
||||
.in_progress_todo_keywords
|
||||
.iter()
|
||||
.map(String::as_str)
|
||||
{
|
||||
let result = tag::<_, _, CustomError<_>>(todo_keyword)(input);
|
||||
match result {
|
||||
Ok((remaining, ent)) => {
|
||||
return Ok((remaining, (TodoKeywordType::Todo, ent)));
|
||||
}
|
||||
Err(_) => {}
|
||||
}
|
||||
}
|
||||
for todo_keyword in global_settings
|
||||
.complete_todo_keywords
|
||||
.iter()
|
||||
.map(String::as_str)
|
||||
{
|
||||
let result = tag::<_, _, CustomError<_>>(todo_keyword)(input);
|
||||
match result {
|
||||
Ok((remaining, ent)) => {
|
||||
return Ok((remaining, (TodoKeywordType::Done, ent)));
|
||||
}
|
||||
Err(_) => {}
|
||||
}
|
||||
}
|
||||
Err(nom::Err::Error(CustomError::MyError(MyError(
|
||||
"NoTodoKeyword".into(),
|
||||
))))
|
||||
}
|
||||
}
|
||||
|
||||
fn priority_cookie<'s>(input: OrgSource<'s>) -> Res<OrgSource<'s>, PriorityCookie> {
|
||||
let (remaining, (_, priority_character, _)) = tuple((
|
||||
tag("[#"),
|
||||
verify(anychar, |c| c.is_alphanumeric()),
|
||||
tag("]"),
|
||||
))(input)?;
|
||||
let cookie = PriorityCookie::try_from(priority_character).map_err(|_| {
|
||||
nom::Err::Error(CustomError::MyError(MyError(
|
||||
"Failed to cast priority cookie to number.".into(),
|
||||
)))
|
||||
})?;
|
||||
Ok((remaining, cookie))
|
||||
}
|
||||
@@ -9,14 +9,14 @@ use nom::multi::many1_count;
|
||||
use nom::sequence::tuple;
|
||||
|
||||
use super::org_source::OrgSource;
|
||||
use super::Context;
|
||||
use crate::context::RefContext;
|
||||
use crate::error::Res;
|
||||
use crate::parser::util::start_of_line;
|
||||
use crate::parser::HorizontalRule;
|
||||
use crate::types::HorizontalRule;
|
||||
|
||||
#[cfg_attr(feature = "tracing", tracing::instrument(ret, level = "debug"))]
|
||||
pub fn horizontal_rule<'r, 's>(
|
||||
_context: Context<'r, 's>,
|
||||
pub fn horizontal_rule<'b, 'g, 'r, 's>(
|
||||
_context: RefContext<'b, 'g, 'r, 's>,
|
||||
input: OrgSource<'s>,
|
||||
) -> Res<OrgSource<'s>, HorizontalRule<'s>> {
|
||||
start_of_line(input)?;
|
||||
|
||||
69
src/parser/in_buffer_settings.rs
Normal file
69
src/parser/in_buffer_settings.rs
Normal file
@@ -0,0 +1,69 @@
|
||||
use nom::branch::alt;
|
||||
use nom::bytes::complete::tag_no_case;
|
||||
use nom::character::complete::anychar;
|
||||
use nom::combinator::map;
|
||||
use nom::multi::many0;
|
||||
use nom::multi::many_till;
|
||||
|
||||
use super::keyword::filtered_keyword;
|
||||
use super::keyword_todo::todo_keywords;
|
||||
use super::OrgSource;
|
||||
use crate::error::Res;
|
||||
use crate::types::Keyword;
|
||||
use crate::GlobalSettings;
|
||||
|
||||
#[cfg_attr(feature = "tracing", tracing::instrument(ret, level = "debug"))]
|
||||
pub fn scan_for_in_buffer_settings<'s>(
|
||||
input: OrgSource<'s>,
|
||||
) -> Res<OrgSource<'s>, Vec<Keyword<'s>>> {
|
||||
// TODO: Optimization idea: since this is slicing the OrgSource at each character, it might be more efficient to do a parser that uses a search function like take_until, and wrap it in a function similar to consumed but returning the input along with the normal output, then pass all of that into a verify that confirms we were at the start of a line using the input we just returned.
|
||||
|
||||
let keywords = many0(map(
|
||||
many_till(anychar, filtered_keyword(in_buffer_settings_key)),
|
||||
|(_, kw)| kw,
|
||||
))(input);
|
||||
keywords
|
||||
}
|
||||
|
||||
#[cfg_attr(feature = "tracing", tracing::instrument(ret, level = "debug"))]
|
||||
fn in_buffer_settings_key<'s>(input: OrgSource<'s>) -> Res<OrgSource<'s>, OrgSource<'s>> {
|
||||
alt((
|
||||
tag_no_case("archive"),
|
||||
tag_no_case("category"),
|
||||
tag_no_case("columns"),
|
||||
tag_no_case("filetags"),
|
||||
tag_no_case("link"),
|
||||
tag_no_case("priorities"),
|
||||
tag_no_case("property"),
|
||||
tag_no_case("seq_todo"),
|
||||
tag_no_case("setupfile"),
|
||||
tag_no_case("startup"),
|
||||
tag_no_case("tags"),
|
||||
tag_no_case("todo"),
|
||||
tag_no_case("typ_todo"),
|
||||
))(input)
|
||||
}
|
||||
|
||||
pub fn apply_in_buffer_settings<'g, 's, 'sf>(
|
||||
keywords: Vec<Keyword<'sf>>,
|
||||
original_settings: &'g GlobalSettings<'g, 's>,
|
||||
) -> Result<GlobalSettings<'g, 's>, String> {
|
||||
let mut new_settings = original_settings.clone();
|
||||
|
||||
for kw in keywords.iter().filter(|kw| {
|
||||
kw.key.eq_ignore_ascii_case("todo")
|
||||
|| kw.key.eq_ignore_ascii_case("seq_todo")
|
||||
|| kw.key.eq_ignore_ascii_case("typ_todo")
|
||||
}) {
|
||||
let (_, (in_progress_words, complete_words)) =
|
||||
todo_keywords(kw.value).map_err(|err| err.to_string())?;
|
||||
new_settings
|
||||
.in_progress_todo_keywords
|
||||
.extend(in_progress_words.into_iter().map(str::to_string));
|
||||
new_settings
|
||||
.complete_todo_keywords
|
||||
.extend(complete_words.into_iter().map(str::to_string));
|
||||
}
|
||||
|
||||
Ok(new_settings)
|
||||
}
|
||||
@@ -12,21 +12,22 @@ use nom::multi::many_till;
|
||||
use super::org_source::BracketDepth;
|
||||
use super::org_source::OrgSource;
|
||||
use super::util::maybe_consume_object_trailing_whitespace_if_not_exiting;
|
||||
use super::Context;
|
||||
use crate::context::parser_with_context;
|
||||
use crate::context::ContextElement;
|
||||
use crate::context::ContextMatcher;
|
||||
use crate::context::ExitClass;
|
||||
use crate::context::ExitMatcherNode;
|
||||
use crate::context::RefContext;
|
||||
use crate::error::CustomError;
|
||||
use crate::error::MyError;
|
||||
use crate::error::Res;
|
||||
use crate::parser::exiting::ExitClass;
|
||||
use crate::parser::parser_context::ContextElement;
|
||||
use crate::parser::parser_context::ExitMatcherNode;
|
||||
use crate::parser::parser_with_context::parser_with_context;
|
||||
use crate::parser::util::exit_matcher_parser;
|
||||
use crate::parser::util::get_consumed;
|
||||
use crate::parser::InlineBabelCall;
|
||||
use crate::types::InlineBabelCall;
|
||||
|
||||
#[cfg_attr(feature = "tracing", tracing::instrument(ret, level = "debug"))]
|
||||
pub fn inline_babel_call<'r, 's>(
|
||||
context: Context<'r, 's>,
|
||||
pub fn inline_babel_call<'b, 'g, 'r, 's>(
|
||||
context: RefContext<'b, 'g, 'r, 's>,
|
||||
input: OrgSource<'s>,
|
||||
) -> Res<OrgSource<'s>, InlineBabelCall<'s>> {
|
||||
let (remaining, _) = tag_no_case("call_")(input)?;
|
||||
@@ -46,15 +47,15 @@ pub fn inline_babel_call<'r, 's>(
|
||||
}
|
||||
|
||||
#[cfg_attr(feature = "tracing", tracing::instrument(ret, level = "debug"))]
|
||||
fn name<'r, 's>(
|
||||
context: Context<'r, 's>,
|
||||
fn name<'b, 'g, 'r, 's>(
|
||||
context: RefContext<'b, 'g, 'r, 's>,
|
||||
input: OrgSource<'s>,
|
||||
) -> Res<OrgSource<'s>, OrgSource<'s>> {
|
||||
let parser_context =
|
||||
context.with_additional_node(ContextElement::ExitMatcherNode(ExitMatcherNode {
|
||||
class: ExitClass::Gamma,
|
||||
exit_matcher: &name_end,
|
||||
}));
|
||||
let parser_context = ContextElement::ExitMatcherNode(ExitMatcherNode {
|
||||
class: ExitClass::Gamma,
|
||||
exit_matcher: &name_end,
|
||||
});
|
||||
let parser_context = context.with_additional_node(&parser_context);
|
||||
let (remaining, name) = recognize(many_till(
|
||||
verify(anychar, |c| !(c.is_whitespace() || "[]()".contains(*c))),
|
||||
parser_with_context!(exit_matcher_parser)(&parser_context),
|
||||
@@ -63,26 +64,26 @@ fn name<'r, 's>(
|
||||
}
|
||||
|
||||
#[cfg_attr(feature = "tracing", tracing::instrument(ret, level = "debug"))]
|
||||
fn name_end<'r, 's>(
|
||||
_context: Context<'r, 's>,
|
||||
fn name_end<'b, 'g, 'r, 's>(
|
||||
_context: RefContext<'b, 'g, 'r, 's>,
|
||||
input: OrgSource<'s>,
|
||||
) -> Res<OrgSource<'s>, OrgSource<'s>> {
|
||||
recognize(one_of("[("))(input)
|
||||
}
|
||||
|
||||
#[cfg_attr(feature = "tracing", tracing::instrument(ret, level = "debug"))]
|
||||
fn header<'r, 's>(
|
||||
context: Context<'r, 's>,
|
||||
fn header<'b, 'g, 'r, 's>(
|
||||
context: RefContext<'b, 'g, 'r, 's>,
|
||||
input: OrgSource<'s>,
|
||||
) -> Res<OrgSource<'s>, OrgSource<'s>> {
|
||||
let (remaining, _) = tag("[")(input)?;
|
||||
|
||||
let exit_with_depth = header_end(remaining.get_bracket_depth());
|
||||
let parser_context =
|
||||
context.with_additional_node(ContextElement::ExitMatcherNode(ExitMatcherNode {
|
||||
class: ExitClass::Gamma,
|
||||
exit_matcher: &exit_with_depth,
|
||||
}));
|
||||
let parser_context = ContextElement::ExitMatcherNode(ExitMatcherNode {
|
||||
class: ExitClass::Gamma,
|
||||
exit_matcher: &exit_with_depth,
|
||||
});
|
||||
let parser_context = context.with_additional_node(&parser_context);
|
||||
|
||||
let (remaining, name) = recognize(many_till(
|
||||
anychar,
|
||||
@@ -92,17 +93,13 @@ fn header<'r, 's>(
|
||||
Ok((remaining, name))
|
||||
}
|
||||
|
||||
fn header_end(
|
||||
starting_bracket_depth: BracketDepth,
|
||||
) -> impl for<'r, 's> Fn(Context<'r, 's>, OrgSource<'s>) -> Res<OrgSource<'s>, OrgSource<'s>> {
|
||||
move |context: Context, input: OrgSource<'_>| {
|
||||
_header_end(context, input, starting_bracket_depth)
|
||||
}
|
||||
fn header_end(starting_bracket_depth: BracketDepth) -> impl ContextMatcher {
|
||||
move |context, input: OrgSource<'_>| _header_end(context, input, starting_bracket_depth)
|
||||
}
|
||||
|
||||
#[cfg_attr(feature = "tracing", tracing::instrument(ret, level = "debug"))]
|
||||
fn _header_end<'r, 's>(
|
||||
_context: Context<'r, 's>,
|
||||
fn _header_end<'b, 'g, 'r, 's>(
|
||||
_context: RefContext<'b, 'g, 'r, 's>,
|
||||
input: OrgSource<'s>,
|
||||
starting_bracket_depth: BracketDepth,
|
||||
) -> Res<OrgSource<'s>, OrgSource<'s>> {
|
||||
@@ -121,18 +118,18 @@ fn _header_end<'r, 's>(
|
||||
}
|
||||
|
||||
#[cfg_attr(feature = "tracing", tracing::instrument(ret, level = "debug"))]
|
||||
fn argument<'r, 's>(
|
||||
context: Context<'r, 's>,
|
||||
fn argument<'b, 'g, 'r, 's>(
|
||||
context: RefContext<'b, 'g, 'r, 's>,
|
||||
input: OrgSource<'s>,
|
||||
) -> Res<OrgSource<'s>, OrgSource<'s>> {
|
||||
let (remaining, _) = tag("(")(input)?;
|
||||
|
||||
let exit_with_depth = argument_end(remaining.get_parenthesis_depth());
|
||||
let parser_context =
|
||||
context.with_additional_node(ContextElement::ExitMatcherNode(ExitMatcherNode {
|
||||
class: ExitClass::Gamma,
|
||||
exit_matcher: &exit_with_depth,
|
||||
}));
|
||||
let parser_context = ContextElement::ExitMatcherNode(ExitMatcherNode {
|
||||
class: ExitClass::Gamma,
|
||||
exit_matcher: &exit_with_depth,
|
||||
});
|
||||
let parser_context = context.with_additional_node(&parser_context);
|
||||
|
||||
let (remaining, name) = recognize(many_till(
|
||||
anychar,
|
||||
@@ -142,17 +139,13 @@ fn argument<'r, 's>(
|
||||
Ok((remaining, name))
|
||||
}
|
||||
|
||||
fn argument_end(
|
||||
starting_parenthesis_depth: BracketDepth,
|
||||
) -> impl for<'r, 's> Fn(Context<'r, 's>, OrgSource<'s>) -> Res<OrgSource<'s>, OrgSource<'s>> {
|
||||
move |context: Context, input: OrgSource<'_>| {
|
||||
_argument_end(context, input, starting_parenthesis_depth)
|
||||
}
|
||||
fn argument_end(starting_parenthesis_depth: BracketDepth) -> impl ContextMatcher {
|
||||
move |context, input: OrgSource<'_>| _argument_end(context, input, starting_parenthesis_depth)
|
||||
}
|
||||
|
||||
#[cfg_attr(feature = "tracing", tracing::instrument(ret, level = "debug"))]
|
||||
fn _argument_end<'r, 's>(
|
||||
_context: Context<'r, 's>,
|
||||
fn _argument_end<'b, 'g, 'r, 's>(
|
||||
_context: RefContext<'b, 'g, 'r, 's>,
|
||||
input: OrgSource<'s>,
|
||||
starting_parenthesis_depth: BracketDepth,
|
||||
) -> Res<OrgSource<'s>, OrgSource<'s>> {
|
||||
|
||||
@@ -14,21 +14,22 @@ use tracing::span;
|
||||
use super::org_source::BracketDepth;
|
||||
use super::org_source::OrgSource;
|
||||
use super::util::maybe_consume_object_trailing_whitespace_if_not_exiting;
|
||||
use super::Context;
|
||||
use crate::context::parser_with_context;
|
||||
use crate::context::ContextElement;
|
||||
use crate::context::ContextMatcher;
|
||||
use crate::context::ExitClass;
|
||||
use crate::context::ExitMatcherNode;
|
||||
use crate::context::RefContext;
|
||||
use crate::error::CustomError;
|
||||
use crate::error::MyError;
|
||||
use crate::error::Res;
|
||||
use crate::parser::exiting::ExitClass;
|
||||
use crate::parser::parser_context::ContextElement;
|
||||
use crate::parser::parser_context::ExitMatcherNode;
|
||||
use crate::parser::parser_with_context::parser_with_context;
|
||||
use crate::parser::util::exit_matcher_parser;
|
||||
use crate::parser::util::get_consumed;
|
||||
use crate::parser::InlineSourceBlock;
|
||||
use crate::types::InlineSourceBlock;
|
||||
|
||||
#[cfg_attr(feature = "tracing", tracing::instrument(ret, level = "debug"))]
|
||||
pub fn inline_source_block<'r, 's>(
|
||||
context: Context<'r, 's>,
|
||||
pub fn inline_source_block<'b, 'g, 'r, 's>(
|
||||
context: RefContext<'b, 'g, 'r, 's>,
|
||||
input: OrgSource<'s>,
|
||||
) -> Res<OrgSource<'s>, InlineSourceBlock<'s>> {
|
||||
let (remaining, _) = tag_no_case("src_")(input)?;
|
||||
@@ -47,15 +48,15 @@ pub fn inline_source_block<'r, 's>(
|
||||
}
|
||||
|
||||
#[cfg_attr(feature = "tracing", tracing::instrument(ret, level = "debug"))]
|
||||
fn lang<'r, 's>(
|
||||
context: Context<'r, 's>,
|
||||
fn lang<'b, 'g, 'r, 's>(
|
||||
context: RefContext<'b, 'g, 'r, 's>,
|
||||
input: OrgSource<'s>,
|
||||
) -> Res<OrgSource<'s>, OrgSource<'s>> {
|
||||
let parser_context =
|
||||
context.with_additional_node(ContextElement::ExitMatcherNode(ExitMatcherNode {
|
||||
class: ExitClass::Beta,
|
||||
exit_matcher: &lang_end,
|
||||
}));
|
||||
let parser_context = ContextElement::ExitMatcherNode(ExitMatcherNode {
|
||||
class: ExitClass::Beta,
|
||||
exit_matcher: &lang_end,
|
||||
});
|
||||
let parser_context = context.with_additional_node(&parser_context);
|
||||
let (remaining, lang) = recognize(many_till(
|
||||
verify(anychar, |c| !(c.is_whitespace() || "[{".contains(*c))),
|
||||
parser_with_context!(exit_matcher_parser)(&parser_context),
|
||||
@@ -64,26 +65,26 @@ fn lang<'r, 's>(
|
||||
}
|
||||
|
||||
#[cfg_attr(feature = "tracing", tracing::instrument(ret, level = "debug"))]
|
||||
fn lang_end<'r, 's>(
|
||||
_context: Context<'r, 's>,
|
||||
fn lang_end<'b, 'g, 'r, 's>(
|
||||
_context: RefContext<'b, 'g, 'r, 's>,
|
||||
input: OrgSource<'s>,
|
||||
) -> Res<OrgSource<'s>, OrgSource<'s>> {
|
||||
recognize(one_of("[{"))(input)
|
||||
}
|
||||
|
||||
#[cfg_attr(feature = "tracing", tracing::instrument(ret, level = "debug"))]
|
||||
fn header<'r, 's>(
|
||||
context: Context<'r, 's>,
|
||||
fn header<'b, 'g, 'r, 's>(
|
||||
context: RefContext<'b, 'g, 'r, 's>,
|
||||
input: OrgSource<'s>,
|
||||
) -> Res<OrgSource<'s>, OrgSource<'s>> {
|
||||
let (remaining, _) = tag("[")(input)?;
|
||||
|
||||
let exit_with_depth = header_end(remaining.get_bracket_depth());
|
||||
let parser_context =
|
||||
context.with_additional_node(ContextElement::ExitMatcherNode(ExitMatcherNode {
|
||||
class: ExitClass::Beta,
|
||||
exit_matcher: &exit_with_depth,
|
||||
}));
|
||||
let parser_context = ContextElement::ExitMatcherNode(ExitMatcherNode {
|
||||
class: ExitClass::Beta,
|
||||
exit_matcher: &exit_with_depth,
|
||||
});
|
||||
let parser_context = context.with_additional_node(&parser_context);
|
||||
|
||||
let (remaining, header_contents) = recognize(many_till(
|
||||
anychar,
|
||||
@@ -93,17 +94,13 @@ fn header<'r, 's>(
|
||||
Ok((remaining, header_contents))
|
||||
}
|
||||
|
||||
fn header_end(
|
||||
starting_bracket_depth: BracketDepth,
|
||||
) -> impl for<'r, 's> Fn(Context<'r, 's>, OrgSource<'s>) -> Res<OrgSource<'s>, OrgSource<'s>> {
|
||||
move |context: Context, input: OrgSource<'_>| {
|
||||
_header_end(context, input, starting_bracket_depth)
|
||||
}
|
||||
fn header_end(starting_bracket_depth: BracketDepth) -> impl ContextMatcher {
|
||||
move |context, input: OrgSource<'_>| _header_end(context, input, starting_bracket_depth)
|
||||
}
|
||||
|
||||
#[cfg_attr(feature = "tracing", tracing::instrument(ret, level = "debug"))]
|
||||
fn _header_end<'r, 's>(
|
||||
_context: Context<'r, 's>,
|
||||
fn _header_end<'b, 'g, 'r, 's>(
|
||||
_context: RefContext<'b, 'g, 'r, 's>,
|
||||
input: OrgSource<'s>,
|
||||
starting_bracket_depth: BracketDepth,
|
||||
) -> Res<OrgSource<'s>, OrgSource<'s>> {
|
||||
@@ -122,18 +119,18 @@ fn _header_end<'r, 's>(
|
||||
}
|
||||
|
||||
#[cfg_attr(feature = "tracing", tracing::instrument(ret, level = "debug"))]
|
||||
fn body<'r, 's>(
|
||||
context: Context<'r, 's>,
|
||||
fn body<'b, 'g, 'r, 's>(
|
||||
context: RefContext<'b, 'g, 'r, 's>,
|
||||
input: OrgSource<'s>,
|
||||
) -> Res<OrgSource<'s>, OrgSource<'s>> {
|
||||
let (remaining, _) = tag("{")(input)?;
|
||||
|
||||
let exit_with_depth = body_end(remaining.get_brace_depth());
|
||||
let parser_context =
|
||||
context.with_additional_node(ContextElement::ExitMatcherNode(ExitMatcherNode {
|
||||
class: ExitClass::Beta,
|
||||
exit_matcher: &exit_with_depth,
|
||||
}));
|
||||
let parser_context = ContextElement::ExitMatcherNode(ExitMatcherNode {
|
||||
class: ExitClass::Beta,
|
||||
exit_matcher: &exit_with_depth,
|
||||
});
|
||||
let parser_context = context.with_additional_node(&parser_context);
|
||||
|
||||
let (remaining, body_contents) = recognize(many_till(
|
||||
anychar,
|
||||
@@ -153,15 +150,13 @@ fn body<'r, 's>(
|
||||
Ok((remaining, body_contents))
|
||||
}
|
||||
|
||||
fn body_end(
|
||||
starting_brace_depth: BracketDepth,
|
||||
) -> impl for<'r, 's> Fn(Context<'r, 's>, OrgSource<'s>) -> Res<OrgSource<'s>, OrgSource<'s>> {
|
||||
move |context: Context, input: OrgSource<'_>| _body_end(context, input, starting_brace_depth)
|
||||
fn body_end(starting_brace_depth: BracketDepth) -> impl ContextMatcher {
|
||||
move |context, input: OrgSource<'_>| _body_end(context, input, starting_brace_depth)
|
||||
}
|
||||
|
||||
#[cfg_attr(feature = "tracing", tracing::instrument(ret, level = "debug"))]
|
||||
fn _body_end<'r, 's>(
|
||||
_context: Context<'r, 's>,
|
||||
fn _body_end<'b, 'g, 'r, 's>(
|
||||
_context: RefContext<'b, 'g, 'r, 's>,
|
||||
input: OrgSource<'s>,
|
||||
starting_brace_depth: BracketDepth,
|
||||
) -> Res<OrgSource<'s>, OrgSource<'s>> {
|
||||
|
||||
@@ -5,8 +5,9 @@ use nom::bytes::complete::tag_no_case;
|
||||
use nom::bytes::complete::take_while1;
|
||||
use nom::character::complete::anychar;
|
||||
use nom::character::complete::line_ending;
|
||||
use nom::character::complete::one_of;
|
||||
use nom::character::complete::space0;
|
||||
use nom::character::complete::space1;
|
||||
use nom::combinator::consumed;
|
||||
use nom::combinator::eof;
|
||||
use nom::combinator::not;
|
||||
use nom::combinator::peek;
|
||||
@@ -16,66 +17,124 @@ use nom::sequence::tuple;
|
||||
|
||||
use super::org_source::BracketDepth;
|
||||
use super::org_source::OrgSource;
|
||||
use super::Context;
|
||||
use crate::context::Matcher;
|
||||
use crate::context::RefContext;
|
||||
use crate::error::CustomError;
|
||||
use crate::error::MyError;
|
||||
use crate::error::Res;
|
||||
use crate::parser::util::start_of_line;
|
||||
use crate::parser::Keyword;
|
||||
use crate::types::Keyword;
|
||||
|
||||
const ORG_ELEMENT_AFFILIATED_KEYWORDS: [&'static str; 13] = [
|
||||
"caption", "data", "header", "headers", "label", "name", "plot", "resname", "result",
|
||||
"results", "source", "srcname", "tblname",
|
||||
"caption", "data", "headers", "header", "label", "name", "plot", "resname", "results",
|
||||
"result", "source", "srcname", "tblname",
|
||||
];
|
||||
const ORG_ELEMENT_DUAL_KEYWORDS: [&'static str; 2] = ["caption", "results"];
|
||||
|
||||
#[cfg_attr(feature = "tracing", tracing::instrument(ret, level = "debug"))]
|
||||
pub fn keyword<'r, 's>(
|
||||
_context: Context<'r, 's>,
|
||||
pub fn filtered_keyword<F: Matcher>(
|
||||
key_parser: F,
|
||||
) -> impl for<'s> Fn(OrgSource<'s>) -> Res<OrgSource<'s>, Keyword<'s>> {
|
||||
move |input| _filtered_keyword(&key_parser, input)
|
||||
}
|
||||
|
||||
#[cfg_attr(
|
||||
feature = "tracing",
|
||||
tracing::instrument(ret, level = "debug", skip(key_parser))
|
||||
)]
|
||||
fn _filtered_keyword<'s, F: Matcher>(
|
||||
key_parser: F,
|
||||
input: OrgSource<'s>,
|
||||
) -> Res<OrgSource<'s>, Keyword<'s>> {
|
||||
start_of_line(input)?;
|
||||
// TODO: When key is a member of org-element-parsed-keywords, value can contain the standard set objects, excluding footnote references.
|
||||
let (remaining, rule) = recognize(tuple((
|
||||
space0,
|
||||
tag("#+"),
|
||||
not(peek(tag_no_case("call"))),
|
||||
not(peek(tag_no_case("begin"))),
|
||||
is_not(" \t\r\n:"),
|
||||
tag(":"),
|
||||
alt((recognize(tuple((space1, is_not("\r\n")))), space0)),
|
||||
let (remaining, (consumed_input, (_, _, parsed_key, _))) =
|
||||
consumed(tuple((space0, tag("#+"), key_parser, tag(":"))))(input)?;
|
||||
match tuple((
|
||||
space0::<OrgSource<'_>, CustomError<OrgSource<'_>>>,
|
||||
alt((line_ending, eof)),
|
||||
)))(input)?;
|
||||
))(remaining)
|
||||
{
|
||||
Ok((remaining, _)) => {
|
||||
return Ok((
|
||||
remaining,
|
||||
Keyword {
|
||||
source: consumed_input.into(),
|
||||
key: parsed_key.into(),
|
||||
value: "".into(),
|
||||
},
|
||||
));
|
||||
}
|
||||
Err(_) => {}
|
||||
};
|
||||
let (remaining, _ws) = space0(remaining)?;
|
||||
let (remaining, parsed_value) = recognize(many_till(
|
||||
anychar,
|
||||
peek(tuple((space0, alt((line_ending, eof))))),
|
||||
))(remaining)?;
|
||||
let (remaining, _ws) = tuple((space0, alt((line_ending, eof))))(remaining)?;
|
||||
Ok((
|
||||
remaining,
|
||||
Keyword {
|
||||
source: rule.into(),
|
||||
source: consumed_input.into(),
|
||||
key: parsed_key.into(),
|
||||
value: parsed_value.into(),
|
||||
},
|
||||
))
|
||||
}
|
||||
|
||||
#[cfg_attr(feature = "tracing", tracing::instrument(ret, level = "debug"))]
|
||||
pub fn affiliated_keyword<'r, 's>(
|
||||
_context: Context<'r, 's>,
|
||||
pub fn keyword<'b, 'g, 'r, 's>(
|
||||
_context: RefContext<'b, 'g, 'r, 's>,
|
||||
input: OrgSource<'s>,
|
||||
) -> Res<OrgSource<'s>, Keyword<'s>> {
|
||||
start_of_line(input)?;
|
||||
filtered_keyword(regular_keyword_key)(input)
|
||||
}
|
||||
|
||||
// TODO: When key is a member of org-element-parsed-keywords, value can contain the standard set objects, excluding footnote references.
|
||||
let (remaining, rule) = recognize(tuple((
|
||||
space0,
|
||||
tag("#+"),
|
||||
affiliated_key,
|
||||
tag(":"),
|
||||
alt((recognize(tuple((space1, is_not("\r\n")))), space0)),
|
||||
alt((line_ending, eof)),
|
||||
)))(input)?;
|
||||
Ok((
|
||||
remaining,
|
||||
Keyword {
|
||||
source: rule.into(),
|
||||
},
|
||||
))
|
||||
#[cfg_attr(feature = "tracing", tracing::instrument(ret, level = "debug"))]
|
||||
pub fn affiliated_keyword<'b, 'g, 'r, 's>(
|
||||
_context: RefContext<'b, 'g, 'r, 's>,
|
||||
input: OrgSource<'s>,
|
||||
) -> Res<OrgSource<'s>, Keyword<'s>> {
|
||||
filtered_keyword(affiliated_key)(input)
|
||||
}
|
||||
|
||||
#[cfg_attr(feature = "tracing", tracing::instrument(ret, level = "debug"))]
|
||||
pub fn babel_call_keyword<'b, 'g, 'r, 's>(
|
||||
_context: RefContext<'b, 'g, 'r, 's>,
|
||||
input: OrgSource<'s>,
|
||||
) -> Res<OrgSource<'s>, Keyword<'s>> {
|
||||
filtered_keyword(babel_call_key)(input)
|
||||
}
|
||||
|
||||
#[cfg_attr(feature = "tracing", tracing::instrument(ret, level = "debug"))]
|
||||
fn babel_call_key<'s>(input: OrgSource<'s>) -> Res<OrgSource<'s>, OrgSource<'s>> {
|
||||
tag_no_case("call")(input)
|
||||
}
|
||||
|
||||
#[cfg_attr(feature = "tracing", tracing::instrument(ret, level = "debug"))]
|
||||
pub fn table_formula_keyword<'b, 'g, 'r, 's>(
|
||||
_context: RefContext<'b, 'g, 'r, 's>,
|
||||
input: OrgSource<'s>,
|
||||
) -> Res<OrgSource<'s>, Keyword<'s>> {
|
||||
filtered_keyword(table_formula_key)(input)
|
||||
}
|
||||
|
||||
#[cfg_attr(feature = "tracing", tracing::instrument(ret, level = "debug"))]
|
||||
fn table_formula_key<'s>(input: OrgSource<'s>) -> Res<OrgSource<'s>, OrgSource<'s>> {
|
||||
tag_no_case("tblfm")(input)
|
||||
}
|
||||
|
||||
#[cfg_attr(feature = "tracing", tracing::instrument(ret, level = "debug"))]
|
||||
fn regular_keyword_key<'s>(input: OrgSource<'s>) -> Res<OrgSource<'s>, OrgSource<'s>> {
|
||||
not(peek(alt((tag_no_case("call"), tag_no_case("begin")))))(input)?;
|
||||
recognize(many_till(
|
||||
anychar,
|
||||
peek(alt((
|
||||
recognize(one_of(" \t\r\n")), // Give up if we hit whitespace
|
||||
recognize(tuple((tag(":"), one_of(" \t\r\n")))), // Stop if we see a colon followed by whitespace
|
||||
recognize(tuple((tag(":"), is_not(" \t\r\n:"), not(tag(":"))))), // Stop if we see a colon that is the last colon before whitespace. This is for keywords like "#+foo:bar:baz: lorem: ipsum" which would have the key "foo:bar:baz".
|
||||
))),
|
||||
))(input)
|
||||
}
|
||||
|
||||
#[cfg_attr(feature = "tracing", tracing::instrument(ret, level = "debug"))]
|
||||
|
||||
94
src/parser/keyword_todo.rs
Normal file
94
src/parser/keyword_todo.rs
Normal file
@@ -0,0 +1,94 @@
|
||||
use nom::branch::alt;
|
||||
use nom::bytes::complete::tag;
|
||||
use nom::bytes::complete::take_till;
|
||||
use nom::character::complete::line_ending;
|
||||
use nom::character::complete::space0;
|
||||
use nom::character::complete::space1;
|
||||
use nom::combinator::eof;
|
||||
use nom::combinator::opt;
|
||||
use nom::combinator::verify;
|
||||
use nom::multi::separated_list0;
|
||||
use nom::sequence::tuple;
|
||||
|
||||
use crate::error::Res;
|
||||
|
||||
// ref https://orgmode.org/manual/Per_002dfile-keywords.html
|
||||
// ref https://orgmode.org/manual/Workflow-states.html
|
||||
// Case is significant.
|
||||
|
||||
/// Parses the text in the value of a #+TODO keyword.
|
||||
///
|
||||
/// Example input: "foo bar baz | lorem ipsum"
|
||||
pub fn todo_keywords<'s>(input: &'s str) -> Res<&'s str, (Vec<&'s str>, Vec<&'s str>)> {
|
||||
let (remaining, mut before_pipe_words) = separated_list0(space1, todo_keyword_word)(input)?;
|
||||
let (remaining, after_pipe_words) = opt(tuple((
|
||||
tuple((space0, tag("|"), space0)),
|
||||
separated_list0(space1, todo_keyword_word),
|
||||
)))(remaining)?;
|
||||
let (remaining, _eol) = alt((line_ending, eof))(remaining)?;
|
||||
if let Some((_pipe, after_pipe_words)) = after_pipe_words {
|
||||
Ok((remaining, (before_pipe_words, after_pipe_words)))
|
||||
} else if !before_pipe_words.is_empty() {
|
||||
// If there was no pipe, then the last word becomes a completion state instead.
|
||||
let mut after_pipe_words = Vec::with_capacity(1);
|
||||
after_pipe_words.push(
|
||||
before_pipe_words
|
||||
.pop()
|
||||
.expect("If-statement proves this is Some."),
|
||||
);
|
||||
Ok((remaining, (before_pipe_words, after_pipe_words)))
|
||||
} else {
|
||||
// No words founds
|
||||
Ok((remaining, (Vec::new(), Vec::new())))
|
||||
}
|
||||
}
|
||||
|
||||
fn todo_keyword_word<'s>(input: &'s str) -> Res<&'s str, &'s str> {
|
||||
verify(take_till(|c| " \t\r\n|".contains(c)), |result: &str| {
|
||||
!result.is_empty()
|
||||
})(input)
|
||||
}
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn before_and_after() -> Result<(), Box<dyn std::error::Error>> {
|
||||
let input = "foo bar baz | lorem ipsum";
|
||||
let (remaining, (before_pipe_words, after_pipe_words)) = todo_keywords(input)?;
|
||||
assert_eq!(remaining, "");
|
||||
assert_eq!(before_pipe_words, vec!["foo", "bar", "baz"]);
|
||||
assert_eq!(after_pipe_words, vec!["lorem", "ipsum"]);
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn no_pipe() -> Result<(), Box<dyn std::error::Error>> {
|
||||
let input = "foo bar baz";
|
||||
let (remaining, (before_pipe_words, after_pipe_words)) = todo_keywords(input)?;
|
||||
assert_eq!(remaining, "");
|
||||
assert_eq!(before_pipe_words, vec!["foo", "bar"]);
|
||||
assert_eq!(after_pipe_words, vec!["baz"]);
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn early_pipe() -> Result<(), Box<dyn std::error::Error>> {
|
||||
let input = "| foo bar baz";
|
||||
let (remaining, (before_pipe_words, after_pipe_words)) = todo_keywords(input)?;
|
||||
assert_eq!(remaining, "");
|
||||
assert_eq!(before_pipe_words, Vec::<&str>::new());
|
||||
assert_eq!(after_pipe_words, vec!["foo", "bar", "baz"]);
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn late_pipe() -> Result<(), Box<dyn std::error::Error>> {
|
||||
let input = "foo bar baz |";
|
||||
let (remaining, (before_pipe_words, after_pipe_words)) = todo_keywords(input)?;
|
||||
assert_eq!(remaining, "");
|
||||
assert_eq!(before_pipe_words, vec!["foo", "bar", "baz"]);
|
||||
assert_eq!(after_pipe_words, Vec::<&str>::new());
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
@@ -13,19 +13,20 @@ use nom::sequence::tuple;
|
||||
|
||||
use super::org_source::OrgSource;
|
||||
use super::util::get_consumed;
|
||||
use super::Context;
|
||||
use crate::context::parser_with_context;
|
||||
use crate::context::ContextElement;
|
||||
use crate::context::ContextMatcher;
|
||||
use crate::context::ExitClass;
|
||||
use crate::context::ExitMatcherNode;
|
||||
use crate::context::RefContext;
|
||||
use crate::error::Res;
|
||||
use crate::parser::exiting::ExitClass;
|
||||
use crate::parser::parser_context::ContextElement;
|
||||
use crate::parser::parser_context::ExitMatcherNode;
|
||||
use crate::parser::parser_with_context::parser_with_context;
|
||||
use crate::parser::util::exit_matcher_parser;
|
||||
use crate::parser::util::start_of_line;
|
||||
use crate::parser::LatexEnvironment;
|
||||
use crate::types::LatexEnvironment;
|
||||
|
||||
#[cfg_attr(feature = "tracing", tracing::instrument(ret, level = "debug"))]
|
||||
pub fn latex_environment<'r, 's>(
|
||||
context: Context<'r, 's>,
|
||||
pub fn latex_environment<'b, 'g, 'r, 's>(
|
||||
context: RefContext<'b, 'g, 'r, 's>,
|
||||
input: OrgSource<'s>,
|
||||
) -> Res<OrgSource<'s>, LatexEnvironment<'s>> {
|
||||
start_of_line(input)?;
|
||||
@@ -39,13 +40,13 @@ pub fn latex_environment<'r, 's>(
|
||||
))(remaining)?;
|
||||
|
||||
let latex_environment_end_specialized = latex_environment_end(name.into());
|
||||
let parser_context =
|
||||
context.with_additional_node(ContextElement::ExitMatcherNode(ExitMatcherNode {
|
||||
class: ExitClass::Beta,
|
||||
exit_matcher: &latex_environment_end_specialized,
|
||||
}));
|
||||
let parser_context = ContextElement::ExitMatcherNode(ExitMatcherNode {
|
||||
class: ExitClass::Beta,
|
||||
exit_matcher: &latex_environment_end_specialized,
|
||||
});
|
||||
let parser_context = context.with_additional_node(&parser_context);
|
||||
|
||||
let (remaining, _contents) = contents(&latex_environment_end_specialized, context, remaining)?;
|
||||
let (remaining, _contents) = contents(&latex_environment_end_specialized)(context, remaining)?;
|
||||
let (remaining, _end) = latex_environment_end_specialized(&parser_context, remaining)?;
|
||||
|
||||
let source = get_consumed(input, remaining);
|
||||
@@ -62,44 +63,42 @@ fn name<'s>(input: OrgSource<'s>) -> Res<OrgSource<'s>, OrgSource<'s>> {
|
||||
take_while1(|c: char| c.is_alphanumeric() || c == '*')(input)
|
||||
}
|
||||
|
||||
fn contents<F: ContextMatcher>(end_matcher: F) -> impl ContextMatcher {
|
||||
move |context, input| _contents(&end_matcher, context, input)
|
||||
}
|
||||
|
||||
#[cfg_attr(
|
||||
feature = "tracing",
|
||||
tracing::instrument(ret, level = "debug", skip(end_matcher))
|
||||
)]
|
||||
pub fn contents<
|
||||
'r,
|
||||
's,
|
||||
F: Fn(Context<'r, 's>, OrgSource<'s>) -> Res<OrgSource<'s>, OrgSource<'s>>,
|
||||
>(
|
||||
fn _contents<'b, 'g, 'r, 's, F: ContextMatcher>(
|
||||
end_matcher: F,
|
||||
context: Context<'r, 's>,
|
||||
context: RefContext<'b, 'g, 'r, 's>,
|
||||
input: OrgSource<'s>,
|
||||
) -> Res<OrgSource<'s>, OrgSource<'s>> {
|
||||
let (remaining, source) = recognize(many_till(
|
||||
anychar,
|
||||
peek(alt((
|
||||
parser_with_context!(exit_matcher_parser)(context),
|
||||
parser_with_context!(end_matcher)(context),
|
||||
parser_with_context!(&end_matcher)(context),
|
||||
))),
|
||||
))(input)?;
|
||||
|
||||
Ok((remaining, source))
|
||||
}
|
||||
|
||||
fn latex_environment_end(
|
||||
current_name: &str,
|
||||
) -> impl for<'r, 's> Fn(Context<'r, 's>, OrgSource<'s>) -> Res<OrgSource<'s>, OrgSource<'s>> {
|
||||
fn latex_environment_end(current_name: &str) -> impl ContextMatcher {
|
||||
let current_name_lower = current_name.to_lowercase();
|
||||
move |context: Context, input: OrgSource<'_>| {
|
||||
move |context, input: OrgSource<'_>| {
|
||||
_latex_environment_end(context, input, current_name_lower.as_str())
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg_attr(feature = "tracing", tracing::instrument(ret, level = "debug"))]
|
||||
fn _latex_environment_end<'r, 's, 'x>(
|
||||
_context: Context<'r, 's>,
|
||||
fn _latex_environment_end<'b, 'g, 'r, 's, 'c>(
|
||||
_context: RefContext<'b, 'g, 'r, 's>,
|
||||
input: OrgSource<'s>,
|
||||
current_name_lower: &'x str,
|
||||
current_name_lower: &'c str,
|
||||
) -> Res<OrgSource<'s>, OrgSource<'s>> {
|
||||
start_of_line(input)?;
|
||||
let (remaining, _leading_whitespace) = space0(input)?;
|
||||
|
||||
@@ -14,18 +14,18 @@ use nom::sequence::tuple;
|
||||
|
||||
use super::org_source::OrgSource;
|
||||
use super::util::maybe_consume_object_trailing_whitespace_if_not_exiting;
|
||||
use super::Context;
|
||||
use crate::context::parser_with_context;
|
||||
use crate::context::RefContext;
|
||||
use crate::error::CustomError;
|
||||
use crate::error::MyError;
|
||||
use crate::error::Res;
|
||||
use crate::parser::parser_with_context::parser_with_context;
|
||||
use crate::parser::util::exit_matcher_parser;
|
||||
use crate::parser::util::get_consumed;
|
||||
use crate::parser::LatexFragment;
|
||||
use crate::types::LatexFragment;
|
||||
|
||||
#[cfg_attr(feature = "tracing", tracing::instrument(ret, level = "debug"))]
|
||||
pub fn latex_fragment<'r, 's>(
|
||||
context: Context<'r, 's>,
|
||||
pub fn latex_fragment<'b, 'g, 'r, 's>(
|
||||
context: RefContext<'b, 'g, 'r, 's>,
|
||||
input: OrgSource<'s>,
|
||||
) -> Res<OrgSource<'s>, LatexFragment<'s>> {
|
||||
let (remaining, _) = alt((
|
||||
@@ -48,8 +48,8 @@ pub fn latex_fragment<'r, 's>(
|
||||
}
|
||||
|
||||
#[cfg_attr(feature = "tracing", tracing::instrument(ret, level = "debug"))]
|
||||
fn raw_latex_fragment<'r, 's>(
|
||||
context: Context<'r, 's>,
|
||||
fn raw_latex_fragment<'b, 'g, 'r, 's>(
|
||||
context: RefContext<'b, 'g, 'r, 's>,
|
||||
input: OrgSource<'s>,
|
||||
) -> Res<OrgSource<'s>, OrgSource<'s>> {
|
||||
let (remaining, _) = tag("\\")(input)?;
|
||||
@@ -61,16 +61,16 @@ fn raw_latex_fragment<'r, 's>(
|
||||
}
|
||||
|
||||
#[cfg_attr(feature = "tracing", tracing::instrument(ret, level = "debug"))]
|
||||
fn name<'r, 's>(
|
||||
_context: Context<'r, 's>,
|
||||
fn name<'b, 'g, 'r, 's>(
|
||||
_context: RefContext<'b, 'g, 'r, 's>,
|
||||
input: OrgSource<'s>,
|
||||
) -> Res<OrgSource<'s>, OrgSource<'s>> {
|
||||
alpha1(input)
|
||||
}
|
||||
|
||||
#[cfg_attr(feature = "tracing", tracing::instrument(ret, level = "debug"))]
|
||||
fn brackets<'r, 's>(
|
||||
context: Context<'r, 's>,
|
||||
fn brackets<'b, 'g, 'r, 's>(
|
||||
context: RefContext<'b, 'g, 'r, 's>,
|
||||
input: OrgSource<'s>,
|
||||
) -> Res<OrgSource<'s>, OrgSource<'s>> {
|
||||
let (remaining, body) = alt((
|
||||
@@ -101,8 +101,8 @@ fn brackets<'r, 's>(
|
||||
}
|
||||
|
||||
#[cfg_attr(feature = "tracing", tracing::instrument(ret, level = "debug"))]
|
||||
fn escaped_parenthesis_fragment<'r, 's>(
|
||||
context: Context<'r, 's>,
|
||||
fn escaped_parenthesis_fragment<'b, 'g, 'r, 's>(
|
||||
context: RefContext<'b, 'g, 'r, 's>,
|
||||
input: OrgSource<'s>,
|
||||
) -> Res<OrgSource<'s>, OrgSource<'s>> {
|
||||
let (remaining, _) = tag("\\(")(input)?;
|
||||
@@ -120,8 +120,8 @@ fn escaped_parenthesis_fragment<'r, 's>(
|
||||
}
|
||||
|
||||
#[cfg_attr(feature = "tracing", tracing::instrument(ret, level = "debug"))]
|
||||
fn escaped_bracket_fragment<'r, 's>(
|
||||
context: Context<'r, 's>,
|
||||
fn escaped_bracket_fragment<'b, 'g, 'r, 's>(
|
||||
context: RefContext<'b, 'g, 'r, 's>,
|
||||
input: OrgSource<'s>,
|
||||
) -> Res<OrgSource<'s>, OrgSource<'s>> {
|
||||
let (remaining, _) = tag("\\[")(input)?;
|
||||
@@ -139,8 +139,8 @@ fn escaped_bracket_fragment<'r, 's>(
|
||||
}
|
||||
|
||||
#[cfg_attr(feature = "tracing", tracing::instrument(ret, level = "debug"))]
|
||||
fn double_dollar_fragment<'r, 's>(
|
||||
context: Context<'r, 's>,
|
||||
fn double_dollar_fragment<'b, 'g, 'r, 's>(
|
||||
context: RefContext<'b, 'g, 'r, 's>,
|
||||
input: OrgSource<'s>,
|
||||
) -> Res<OrgSource<'s>, OrgSource<'s>> {
|
||||
// TODO: The documentation on the dollar sign versions is incomplete. Test to figure out what the real requirements are. For example, can this span more than 3 lines and can this contain a single $ since its terminated by $$?
|
||||
@@ -159,8 +159,8 @@ fn double_dollar_fragment<'r, 's>(
|
||||
}
|
||||
|
||||
#[cfg_attr(feature = "tracing", tracing::instrument(ret, level = "debug"))]
|
||||
fn dollar_char_fragment<'r, 's>(
|
||||
context: Context<'r, 's>,
|
||||
fn dollar_char_fragment<'b, 'g, 'r, 's>(
|
||||
context: RefContext<'b, 'g, 'r, 's>,
|
||||
input: OrgSource<'s>,
|
||||
) -> Res<OrgSource<'s>, OrgSource<'s>> {
|
||||
let (_, _) = pre(context, input)?;
|
||||
@@ -174,7 +174,10 @@ fn dollar_char_fragment<'r, 's>(
|
||||
}
|
||||
|
||||
#[cfg_attr(feature = "tracing", tracing::instrument(ret, level = "debug"))]
|
||||
pub fn pre<'r, 's>(_context: Context<'r, 's>, input: OrgSource<'s>) -> Res<OrgSource<'s>, ()> {
|
||||
pub fn pre<'b, 'g, 'r, 's>(
|
||||
_context: RefContext<'b, 'g, 'r, 's>,
|
||||
input: OrgSource<'s>,
|
||||
) -> Res<OrgSource<'s>, ()> {
|
||||
let preceding_character = input.get_preceding_character();
|
||||
if let Some('$') = preceding_character {
|
||||
return Err(nom::Err::Error(CustomError::MyError(MyError(
|
||||
@@ -185,7 +188,10 @@ pub fn pre<'r, 's>(_context: Context<'r, 's>, input: OrgSource<'s>) -> Res<OrgSo
|
||||
}
|
||||
|
||||
#[cfg_attr(feature = "tracing", tracing::instrument(ret, level = "debug"))]
|
||||
pub fn post<'r, 's>(_context: Context<'r, 's>, input: OrgSource<'s>) -> Res<OrgSource<'s>, ()> {
|
||||
pub fn post<'b, 'g, 'r, 's>(
|
||||
_context: RefContext<'b, 'g, 'r, 's>,
|
||||
input: OrgSource<'s>,
|
||||
) -> Res<OrgSource<'s>, ()> {
|
||||
// TODO: What about eof? Test to find out.
|
||||
|
||||
// TODO: Figure out which punctuation characters should be included.
|
||||
@@ -194,8 +200,8 @@ pub fn post<'r, 's>(_context: Context<'r, 's>, input: OrgSource<'s>) -> Res<OrgS
|
||||
}
|
||||
|
||||
#[cfg_attr(feature = "tracing", tracing::instrument(ret, level = "debug"))]
|
||||
fn bordered_dollar_fragment<'r, 's>(
|
||||
context: Context<'r, 's>,
|
||||
fn bordered_dollar_fragment<'b, 'g, 'r, 's>(
|
||||
context: RefContext<'b, 'g, 'r, 's>,
|
||||
input: OrgSource<'s>,
|
||||
) -> Res<OrgSource<'s>, OrgSource<'s>> {
|
||||
let (_, _) = pre(context, input)?;
|
||||
@@ -220,16 +226,16 @@ fn bordered_dollar_fragment<'r, 's>(
|
||||
}
|
||||
|
||||
#[cfg_attr(feature = "tracing", tracing::instrument(ret, level = "debug"))]
|
||||
pub fn open_border<'r, 's>(
|
||||
_context: Context<'r, 's>,
|
||||
pub fn open_border<'b, 'g, 'r, 's>(
|
||||
_context: RefContext<'b, 'g, 'r, 's>,
|
||||
input: OrgSource<'s>,
|
||||
) -> Res<OrgSource<'s>, OrgSource<'s>> {
|
||||
recognize(verify(none_of(".,;$"), |c| !c.is_whitespace()))(input)
|
||||
}
|
||||
|
||||
#[cfg_attr(feature = "tracing", tracing::instrument(ret, level = "debug"))]
|
||||
pub fn close_border<'r, 's>(
|
||||
_context: Context<'r, 's>,
|
||||
pub fn close_border<'b, 'g, 'r, 's>(
|
||||
_context: RefContext<'b, 'g, 'r, 's>,
|
||||
input: OrgSource<'s>,
|
||||
) -> Res<OrgSource<'s>, ()> {
|
||||
let preceding_character = input.get_preceding_character();
|
||||
|
||||
@@ -7,47 +7,53 @@ use nom::character::complete::space1;
|
||||
use nom::combinator::consumed;
|
||||
use nom::combinator::eof;
|
||||
use nom::combinator::opt;
|
||||
use nom::combinator::recognize;
|
||||
use nom::combinator::verify;
|
||||
use nom::multi::many_till;
|
||||
use nom::sequence::tuple;
|
||||
|
||||
use super::org_source::OrgSource;
|
||||
use super::Context;
|
||||
use crate::context::parser_with_context;
|
||||
use crate::context::ContextElement;
|
||||
use crate::context::ContextMatcher;
|
||||
use crate::context::ExitClass;
|
||||
use crate::context::ExitMatcherNode;
|
||||
use crate::context::RefContext;
|
||||
use crate::error::Res;
|
||||
use crate::parser::exiting::ExitClass;
|
||||
use crate::parser::lesser_element::CommentBlock;
|
||||
use crate::parser::lesser_element::ExampleBlock;
|
||||
use crate::parser::lesser_element::ExportBlock;
|
||||
use crate::parser::lesser_element::SrcBlock;
|
||||
use crate::parser::lesser_element::VerseBlock;
|
||||
use crate::parser::object::Object;
|
||||
use crate::parser::object::PlainText;
|
||||
use crate::parser::object_parser::standard_set_object;
|
||||
use crate::parser::parser_context::ContextElement;
|
||||
use crate::parser::parser_context::ExitMatcherNode;
|
||||
use crate::parser::parser_with_context::parser_with_context;
|
||||
use crate::parser::util::blank_line;
|
||||
use crate::parser::util::exit_matcher_parser;
|
||||
use crate::parser::util::get_consumed;
|
||||
use crate::parser::util::start_of_line;
|
||||
use crate::parser::util::text_until_exit;
|
||||
use crate::types::CommentBlock;
|
||||
use crate::types::ExampleBlock;
|
||||
use crate::types::ExportBlock;
|
||||
use crate::types::Object;
|
||||
use crate::types::PlainText;
|
||||
use crate::types::SrcBlock;
|
||||
use crate::types::VerseBlock;
|
||||
|
||||
#[cfg_attr(feature = "tracing", tracing::instrument(ret, level = "debug"))]
|
||||
pub fn verse_block<'r, 's>(
|
||||
context: Context<'r, 's>,
|
||||
pub fn verse_block<'b, 'g, 'r, 's>(
|
||||
context: RefContext<'b, 'g, 'r, 's>,
|
||||
input: OrgSource<'s>,
|
||||
) -> Res<OrgSource<'s>, VerseBlock<'s>> {
|
||||
let (remaining, name) = lesser_block_begin("verse")(context, input)?;
|
||||
let (remaining, parameters) = opt(tuple((space1, data)))(remaining)?;
|
||||
let (remaining, _nl) = line_ending(remaining)?;
|
||||
let (remaining, _nl) = recognize(tuple((space0, line_ending)))(remaining)?;
|
||||
let lesser_block_end_specialized = lesser_block_end("verse");
|
||||
let parser_context = context
|
||||
.with_additional_node(ContextElement::ConsumeTrailingWhitespace(true))
|
||||
.with_additional_node(ContextElement::Context("lesser block"))
|
||||
.with_additional_node(ContextElement::ExitMatcherNode(ExitMatcherNode {
|
||||
class: ExitClass::Beta,
|
||||
let contexts = [
|
||||
ContextElement::ConsumeTrailingWhitespace(true),
|
||||
ContextElement::Context("lesser block"),
|
||||
ContextElement::ExitMatcherNode(ExitMatcherNode {
|
||||
class: ExitClass::Alpha,
|
||||
exit_matcher: &lesser_block_end_specialized,
|
||||
}));
|
||||
}),
|
||||
];
|
||||
let parser_context = context.with_additional_node(&contexts[0]);
|
||||
let parser_context = parser_context.with_additional_node(&contexts[1]);
|
||||
let parser_context = parser_context.with_additional_node(&contexts[2]);
|
||||
let parameters = match parameters {
|
||||
Some((_ws, parameters)) => Some(parameters),
|
||||
None => None,
|
||||
@@ -84,21 +90,25 @@ pub fn verse_block<'r, 's>(
|
||||
}
|
||||
|
||||
#[cfg_attr(feature = "tracing", tracing::instrument(ret, level = "debug"))]
|
||||
pub fn comment_block<'r, 's>(
|
||||
context: Context<'r, 's>,
|
||||
pub fn comment_block<'b, 'g, 'r, 's>(
|
||||
context: RefContext<'b, 'g, 'r, 's>,
|
||||
input: OrgSource<'s>,
|
||||
) -> Res<OrgSource<'s>, CommentBlock<'s>> {
|
||||
let (remaining, name) = lesser_block_begin("comment")(context, input)?;
|
||||
let (remaining, parameters) = opt(tuple((space1, data)))(remaining)?;
|
||||
let (remaining, _nl) = line_ending(remaining)?;
|
||||
let (remaining, _nl) = recognize(tuple((space0, line_ending)))(remaining)?;
|
||||
let lesser_block_end_specialized = lesser_block_end("comment");
|
||||
let parser_context = context
|
||||
.with_additional_node(ContextElement::ConsumeTrailingWhitespace(true))
|
||||
.with_additional_node(ContextElement::Context("lesser block"))
|
||||
.with_additional_node(ContextElement::ExitMatcherNode(ExitMatcherNode {
|
||||
class: ExitClass::Beta,
|
||||
let contexts = [
|
||||
ContextElement::ConsumeTrailingWhitespace(true),
|
||||
ContextElement::Context("lesser block"),
|
||||
ContextElement::ExitMatcherNode(ExitMatcherNode {
|
||||
class: ExitClass::Alpha,
|
||||
exit_matcher: &lesser_block_end_specialized,
|
||||
}));
|
||||
}),
|
||||
];
|
||||
let parser_context = context.with_additional_node(&contexts[0]);
|
||||
let parser_context = parser_context.with_additional_node(&contexts[1]);
|
||||
let parser_context = parser_context.with_additional_node(&contexts[2]);
|
||||
let parameters = match parameters {
|
||||
Some((_ws, parameters)) => Some(parameters),
|
||||
None => None,
|
||||
@@ -120,21 +130,25 @@ pub fn comment_block<'r, 's>(
|
||||
}
|
||||
|
||||
#[cfg_attr(feature = "tracing", tracing::instrument(ret, level = "debug"))]
|
||||
pub fn example_block<'r, 's>(
|
||||
context: Context<'r, 's>,
|
||||
pub fn example_block<'b, 'g, 'r, 's>(
|
||||
context: RefContext<'b, 'g, 'r, 's>,
|
||||
input: OrgSource<'s>,
|
||||
) -> Res<OrgSource<'s>, ExampleBlock<'s>> {
|
||||
let (remaining, _name) = lesser_block_begin("example")(context, input)?;
|
||||
let (remaining, parameters) = opt(tuple((space1, data)))(remaining)?;
|
||||
let (remaining, _nl) = line_ending(remaining)?;
|
||||
let (remaining, _nl) = recognize(tuple((space0, line_ending)))(remaining)?;
|
||||
let lesser_block_end_specialized = lesser_block_end("example");
|
||||
let parser_context = context
|
||||
.with_additional_node(ContextElement::ConsumeTrailingWhitespace(true))
|
||||
.with_additional_node(ContextElement::Context("lesser block"))
|
||||
.with_additional_node(ContextElement::ExitMatcherNode(ExitMatcherNode {
|
||||
class: ExitClass::Beta,
|
||||
let contexts = [
|
||||
ContextElement::ConsumeTrailingWhitespace(true),
|
||||
ContextElement::Context("lesser block"),
|
||||
ContextElement::ExitMatcherNode(ExitMatcherNode {
|
||||
class: ExitClass::Alpha,
|
||||
exit_matcher: &lesser_block_end_specialized,
|
||||
}));
|
||||
}),
|
||||
];
|
||||
let parser_context = context.with_additional_node(&contexts[0]);
|
||||
let parser_context = parser_context.with_additional_node(&contexts[1]);
|
||||
let parser_context = parser_context.with_additional_node(&contexts[2]);
|
||||
let parameters = match parameters {
|
||||
Some((_ws, parameters)) => Some(parameters),
|
||||
None => None,
|
||||
@@ -156,22 +170,26 @@ pub fn example_block<'r, 's>(
|
||||
}
|
||||
|
||||
#[cfg_attr(feature = "tracing", tracing::instrument(ret, level = "debug"))]
|
||||
pub fn export_block<'r, 's>(
|
||||
context: Context<'r, 's>,
|
||||
pub fn export_block<'b, 'g, 'r, 's>(
|
||||
context: RefContext<'b, 'g, 'r, 's>,
|
||||
input: OrgSource<'s>,
|
||||
) -> Res<OrgSource<'s>, ExportBlock<'s>> {
|
||||
let (remaining, name) = lesser_block_begin("export")(context, input)?;
|
||||
// https://orgmode.org/worg/org-syntax.html#Blocks claims that export blocks must have a single word for data but testing shows no data and multi-word data still parses as an export block.
|
||||
let (remaining, parameters) = opt(tuple((space1, data)))(remaining)?;
|
||||
let (remaining, _nl) = line_ending(remaining)?;
|
||||
let (remaining, _nl) = recognize(tuple((space0, line_ending)))(remaining)?;
|
||||
let lesser_block_end_specialized = lesser_block_end("export");
|
||||
let parser_context = context
|
||||
.with_additional_node(ContextElement::ConsumeTrailingWhitespace(true))
|
||||
.with_additional_node(ContextElement::Context("lesser block"))
|
||||
.with_additional_node(ContextElement::ExitMatcherNode(ExitMatcherNode {
|
||||
class: ExitClass::Beta,
|
||||
let contexts = [
|
||||
ContextElement::ConsumeTrailingWhitespace(true),
|
||||
ContextElement::Context("lesser block"),
|
||||
ContextElement::ExitMatcherNode(ExitMatcherNode {
|
||||
class: ExitClass::Alpha,
|
||||
exit_matcher: &lesser_block_end_specialized,
|
||||
}));
|
||||
}),
|
||||
];
|
||||
let parser_context = context.with_additional_node(&contexts[0]);
|
||||
let parser_context = parser_context.with_additional_node(&contexts[1]);
|
||||
let parser_context = parser_context.with_additional_node(&contexts[2]);
|
||||
let parameters = match parameters {
|
||||
Some((_ws, parameters)) => Some(parameters),
|
||||
None => None,
|
||||
@@ -193,22 +211,26 @@ pub fn export_block<'r, 's>(
|
||||
}
|
||||
|
||||
#[cfg_attr(feature = "tracing", tracing::instrument(ret, level = "debug"))]
|
||||
pub fn src_block<'r, 's>(
|
||||
context: Context<'r, 's>,
|
||||
pub fn src_block<'b, 'g, 'r, 's>(
|
||||
context: RefContext<'b, 'g, 'r, 's>,
|
||||
input: OrgSource<'s>,
|
||||
) -> Res<OrgSource<'s>, SrcBlock<'s>> {
|
||||
let (remaining, name) = lesser_block_begin("src")(context, input)?;
|
||||
// https://orgmode.org/worg/org-syntax.html#Blocks claims that data is mandatory and must follow the LANGUAGE SWITCHES ARGUMENTS pattern but testing has shown that no data and incorrect data here will still parse to a src block.
|
||||
let (remaining, parameters) = opt(tuple((space1, data)))(remaining)?;
|
||||
let (remaining, _nl) = line_ending(remaining)?;
|
||||
let (remaining, _nl) = recognize(tuple((space0, line_ending)))(remaining)?;
|
||||
let lesser_block_end_specialized = lesser_block_end("src");
|
||||
let parser_context = context
|
||||
.with_additional_node(ContextElement::ConsumeTrailingWhitespace(true))
|
||||
.with_additional_node(ContextElement::Context("lesser block"))
|
||||
.with_additional_node(ContextElement::ExitMatcherNode(ExitMatcherNode {
|
||||
let contexts = [
|
||||
ContextElement::ConsumeTrailingWhitespace(true),
|
||||
ContextElement::Context("lesser block"),
|
||||
ContextElement::ExitMatcherNode(ExitMatcherNode {
|
||||
class: ExitClass::Alpha,
|
||||
exit_matcher: &lesser_block_end_specialized,
|
||||
}));
|
||||
}),
|
||||
];
|
||||
let parser_context = context.with_additional_node(&contexts[0]);
|
||||
let parser_context = parser_context.with_additional_node(&contexts[1]);
|
||||
let parser_context = parser_context.with_additional_node(&contexts[2]);
|
||||
let parameters = match parameters {
|
||||
Some((_ws, parameters)) => Some(parameters),
|
||||
None => None,
|
||||
@@ -239,26 +261,25 @@ fn data<'s>(input: OrgSource<'s>) -> Res<OrgSource<'s>, OrgSource<'s>> {
|
||||
is_not("\r\n")(input)
|
||||
}
|
||||
|
||||
fn lesser_block_end(
|
||||
current_name: &str,
|
||||
) -> impl for<'r, 's> Fn(Context<'r, 's>, OrgSource<'s>) -> Res<OrgSource<'s>, OrgSource<'s>> {
|
||||
fn lesser_block_end(current_name: &str) -> impl ContextMatcher {
|
||||
let current_name_lower = current_name.to_lowercase();
|
||||
move |context: Context, input: OrgSource<'_>| {
|
||||
move |context, input: OrgSource<'_>| {
|
||||
_lesser_block_end(context, input, current_name_lower.as_str())
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg_attr(feature = "tracing", tracing::instrument(ret, level = "debug"))]
|
||||
fn _lesser_block_end<'r, 's, 'x>(
|
||||
_context: Context<'r, 's>,
|
||||
fn _lesser_block_end<'b, 'g, 'r, 's, 'c>(
|
||||
_context: RefContext<'b, 'g, 'r, 's>,
|
||||
input: OrgSource<'s>,
|
||||
current_name_lower: &'x str,
|
||||
current_name_lower: &'c str,
|
||||
) -> Res<OrgSource<'s>, OrgSource<'s>> {
|
||||
start_of_line(input)?;
|
||||
let (remaining, _leading_whitespace) = space0(input)?;
|
||||
let (remaining, (_begin, _name, _ws)) = tuple((
|
||||
let (remaining, (_begin, _name, _ws, _ending)) = tuple((
|
||||
tag_no_case("#+end_"),
|
||||
tag_no_case(current_name_lower),
|
||||
space0,
|
||||
alt((eof, line_ending)),
|
||||
))(remaining)?;
|
||||
let source = get_consumed(input, remaining);
|
||||
@@ -268,18 +289,16 @@ fn _lesser_block_end<'r, 's, 'x>(
|
||||
/// Parser for the beginning of a lesser block
|
||||
///
|
||||
/// current_name MUST be lowercase. We do not do the conversion ourselves because it is not allowed in a const fn.
|
||||
const fn lesser_block_begin(
|
||||
current_name: &'static str,
|
||||
) -> impl for<'r, 's> Fn(Context<'r, 's>, OrgSource<'s>) -> Res<OrgSource<'s>, OrgSource<'s>> {
|
||||
const fn lesser_block_begin<'c>(current_name: &'c str) -> impl ContextMatcher + 'c {
|
||||
// TODO: Since this is a const fn, is there ANY way to "generate" functions at compile time?
|
||||
move |context: Context, input: OrgSource<'_>| _lesser_block_begin(context, input, current_name)
|
||||
move |context, input: OrgSource<'_>| _lesser_block_begin(context, input, current_name)
|
||||
}
|
||||
|
||||
#[cfg_attr(feature = "tracing", tracing::instrument(ret, level = "debug"))]
|
||||
fn _lesser_block_begin<'r, 's, 'x>(
|
||||
_context: Context<'r, 's>,
|
||||
fn _lesser_block_begin<'b, 'g, 'r, 's, 'c>(
|
||||
_context: RefContext<'b, 'g, 'r, 's>,
|
||||
input: OrgSource<'s>,
|
||||
current_name_lower: &'x str,
|
||||
current_name_lower: &'c str,
|
||||
) -> Res<OrgSource<'s>, OrgSource<'s>> {
|
||||
start_of_line(input)?;
|
||||
let (remaining, _leading_whitespace) = space0(input)?;
|
||||
|
||||
@@ -5,16 +5,16 @@ use nom::combinator::recognize;
|
||||
use nom::multi::many0;
|
||||
|
||||
use super::org_source::OrgSource;
|
||||
use super::Context;
|
||||
use crate::context::RefContext;
|
||||
use crate::error::CustomError;
|
||||
use crate::error::MyError;
|
||||
use crate::error::Res;
|
||||
use crate::parser::util::get_consumed;
|
||||
use crate::parser::LineBreak;
|
||||
use crate::types::LineBreak;
|
||||
|
||||
#[cfg_attr(feature = "tracing", tracing::instrument(ret, level = "debug"))]
|
||||
pub fn line_break<'r, 's>(
|
||||
context: Context<'r, 's>,
|
||||
pub fn line_break<'b, 'g, 'r, 's>(
|
||||
context: RefContext<'b, 'g, 'r, 's>,
|
||||
input: OrgSource<'s>,
|
||||
) -> Res<OrgSource<'s>, LineBreak<'s>> {
|
||||
let (remaining, _) = pre(context, input)?;
|
||||
@@ -31,7 +31,10 @@ pub fn line_break<'r, 's>(
|
||||
}
|
||||
|
||||
#[cfg_attr(feature = "tracing", tracing::instrument(ret, level = "debug"))]
|
||||
fn pre<'r, 's>(_context: Context<'r, 's>, input: OrgSource<'s>) -> Res<OrgSource<'s>, ()> {
|
||||
fn pre<'b, 'g, 'r, 's>(
|
||||
_context: RefContext<'b, 'g, 'r, 's>,
|
||||
input: OrgSource<'s>,
|
||||
) -> Res<OrgSource<'s>, ()> {
|
||||
let preceding_character = input.get_preceding_character();
|
||||
match preceding_character {
|
||||
// If None, we are at the start of the file
|
||||
|
||||
@@ -1,183 +0,0 @@
|
||||
use std::fmt::Debug;
|
||||
use std::rc::Rc;
|
||||
|
||||
#[derive(Debug)]
|
||||
pub struct List<T> {
|
||||
head: Option<Rc<Node<T>>>,
|
||||
}
|
||||
|
||||
impl<T> Clone for List<T> {
|
||||
fn clone(&self) -> Self {
|
||||
List {
|
||||
head: self.head.clone(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone)]
|
||||
pub struct Node<T> {
|
||||
data: T,
|
||||
parent: Option<Rc<Node<T>>>,
|
||||
}
|
||||
|
||||
impl<T> Node<T> {
|
||||
pub fn get_data(&self) -> &T {
|
||||
&self.data
|
||||
}
|
||||
}
|
||||
|
||||
impl<T> List<T> {
|
||||
pub fn new() -> Self {
|
||||
List { head: None }
|
||||
}
|
||||
|
||||
pub fn branch_from(trunk: &Rc<Node<T>>) -> Self {
|
||||
List {
|
||||
head: Some(trunk.clone()),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn push_front(&self, data: T) -> List<T> {
|
||||
List {
|
||||
head: Some(Rc::new(Node {
|
||||
data: data,
|
||||
parent: self.head.clone(),
|
||||
})),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn pop_front(&mut self) -> (Option<T>, List<T>) {
|
||||
match self.head.take() {
|
||||
None => (None, List::new()),
|
||||
Some(popped_node) => {
|
||||
let extracted_node = match Rc::try_unwrap(popped_node) {
|
||||
Ok(node) => node,
|
||||
Err(_) => panic!("try_unwrap failed on Rc in pop_front on List."),
|
||||
};
|
||||
(
|
||||
Some(extracted_node.data),
|
||||
List {
|
||||
head: extracted_node.parent,
|
||||
},
|
||||
)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[allow(dead_code)]
|
||||
pub fn without_front(&self) -> List<T> {
|
||||
List {
|
||||
head: self.head.as_ref().map(|node| node.parent.clone()).flatten(),
|
||||
}
|
||||
}
|
||||
|
||||
#[allow(dead_code)]
|
||||
pub fn get_data(&self) -> Option<&T> {
|
||||
self.head.as_ref().map(|rc_node| &rc_node.data)
|
||||
}
|
||||
|
||||
#[allow(dead_code)]
|
||||
pub fn is_empty(&self) -> bool {
|
||||
self.head.is_none()
|
||||
}
|
||||
|
||||
pub fn ptr_eq(&self, other: &List<T>) -> bool {
|
||||
match (self.head.as_ref(), other.head.as_ref()) {
|
||||
(None, None) => true,
|
||||
(None, Some(_)) | (Some(_), None) => false,
|
||||
(Some(me), Some(them)) => Rc::ptr_eq(me, them),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn iter(&self) -> impl Iterator<Item = &Rc<Node<T>>> {
|
||||
NodeIter {
|
||||
position: &self.head,
|
||||
}
|
||||
}
|
||||
|
||||
#[allow(dead_code)]
|
||||
pub fn iter_until<'a>(&'a self, other: &'a List<T>) -> impl Iterator<Item = &Rc<Node<T>>> {
|
||||
NodeIterUntil {
|
||||
position: &self.head,
|
||||
stop: &other.head,
|
||||
}
|
||||
}
|
||||
|
||||
#[allow(dead_code)]
|
||||
pub fn into_iter_until<'a>(self, other: &'a List<T>) -> impl Iterator<Item = T> + 'a {
|
||||
NodeIntoIterUntil {
|
||||
position: self,
|
||||
stop: &other,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub struct NodeIter<'a, T> {
|
||||
position: &'a Option<Rc<Node<T>>>,
|
||||
}
|
||||
|
||||
impl<'a, T> Iterator for NodeIter<'a, T> {
|
||||
type Item = &'a Rc<Node<T>>;
|
||||
|
||||
fn next(&mut self) -> Option<Self::Item> {
|
||||
let (return_value, next_position) = match &self.position {
|
||||
None => return None,
|
||||
Some(rc_node) => {
|
||||
let next_position = &rc_node.parent;
|
||||
let return_value = rc_node;
|
||||
(return_value, next_position)
|
||||
}
|
||||
};
|
||||
self.position = next_position;
|
||||
Some(return_value)
|
||||
}
|
||||
}
|
||||
|
||||
pub struct NodeIterUntil<'a, T> {
|
||||
position: &'a Option<Rc<Node<T>>>,
|
||||
stop: &'a Option<Rc<Node<T>>>,
|
||||
}
|
||||
|
||||
impl<'a, T> Iterator for NodeIterUntil<'a, T> {
|
||||
type Item = &'a Rc<Node<T>>;
|
||||
|
||||
fn next(&mut self) -> Option<Self::Item> {
|
||||
match (self.position, self.stop) {
|
||||
(_, None) => {}
|
||||
(None, _) => {}
|
||||
(Some(this_rc), Some(stop_rc)) => {
|
||||
if Rc::ptr_eq(this_rc, stop_rc) {
|
||||
return None;
|
||||
}
|
||||
}
|
||||
};
|
||||
let (return_value, next_position) = match &self.position {
|
||||
None => return None,
|
||||
Some(rc_node) => {
|
||||
let next_position = &rc_node.parent;
|
||||
let return_value = rc_node;
|
||||
(return_value, next_position)
|
||||
}
|
||||
};
|
||||
self.position = next_position;
|
||||
Some(return_value)
|
||||
}
|
||||
}
|
||||
|
||||
pub struct NodeIntoIterUntil<'a, T> {
|
||||
position: List<T>,
|
||||
stop: &'a List<T>,
|
||||
}
|
||||
|
||||
impl<'a, T> Iterator for NodeIntoIterUntil<'a, T> {
|
||||
type Item = T;
|
||||
|
||||
fn next(&mut self) -> Option<Self::Item> {
|
||||
if self.position.ptr_eq(self.stop) {
|
||||
return None;
|
||||
}
|
||||
let (popped_element, new_position) = self.position.pop_front();
|
||||
self.position = new_position;
|
||||
popped_element
|
||||
}
|
||||
}
|
||||
@@ -7,33 +7,28 @@ mod diary_sexp;
|
||||
mod document;
|
||||
mod drawer;
|
||||
mod dynamic_block;
|
||||
mod element;
|
||||
mod element_parser;
|
||||
mod entity;
|
||||
mod exiting;
|
||||
mod export_snippet;
|
||||
mod fixed_width_area;
|
||||
mod footnote_definition;
|
||||
mod footnote_reference;
|
||||
mod greater_block;
|
||||
mod greater_element;
|
||||
mod headline;
|
||||
mod horizontal_rule;
|
||||
mod in_buffer_settings;
|
||||
mod inline_babel_call;
|
||||
mod inline_source_block;
|
||||
mod keyword;
|
||||
mod keyword_todo;
|
||||
mod latex_environment;
|
||||
mod latex_fragment;
|
||||
mod lesser_block;
|
||||
mod lesser_element;
|
||||
mod line_break;
|
||||
mod list;
|
||||
mod object;
|
||||
mod object_parser;
|
||||
mod org_macro;
|
||||
mod org_source;
|
||||
mod paragraph;
|
||||
mod parser_context;
|
||||
mod parser_with_context;
|
||||
mod plain_link;
|
||||
mod plain_list;
|
||||
mod plain_text;
|
||||
@@ -41,8 +36,8 @@ mod planning;
|
||||
mod property_drawer;
|
||||
mod radio_link;
|
||||
mod regular_link;
|
||||
mod section;
|
||||
pub mod sexp;
|
||||
mod source;
|
||||
mod statistics_cookie;
|
||||
mod subscript_and_superscript;
|
||||
mod table;
|
||||
@@ -52,62 +47,6 @@ mod timestamp;
|
||||
mod token;
|
||||
mod util;
|
||||
pub use document::document;
|
||||
pub use document::Document;
|
||||
pub use document::DocumentElement;
|
||||
pub use document::Heading;
|
||||
pub use document::Section;
|
||||
pub use element::Element;
|
||||
pub use greater_element::Drawer;
|
||||
pub use greater_element::DynamicBlock;
|
||||
pub use greater_element::FootnoteDefinition;
|
||||
pub use greater_element::GreaterBlock;
|
||||
pub use greater_element::PlainList;
|
||||
pub use greater_element::PlainListItem;
|
||||
pub use greater_element::PropertyDrawer;
|
||||
pub use greater_element::Table;
|
||||
pub use greater_element::TableRow;
|
||||
pub use lesser_element::Clock;
|
||||
pub use lesser_element::Comment;
|
||||
pub use lesser_element::CommentBlock;
|
||||
pub use lesser_element::DiarySexp;
|
||||
pub use lesser_element::ExampleBlock;
|
||||
pub use lesser_element::ExportBlock;
|
||||
pub use lesser_element::FixedWidthArea;
|
||||
pub use lesser_element::HorizontalRule;
|
||||
pub use lesser_element::Keyword;
|
||||
pub use lesser_element::LatexEnvironment;
|
||||
pub use lesser_element::Paragraph;
|
||||
pub use lesser_element::Planning;
|
||||
pub use lesser_element::SrcBlock;
|
||||
pub use lesser_element::TableCell;
|
||||
pub use lesser_element::VerseBlock;
|
||||
pub use object::AngleLink;
|
||||
pub use object::Bold;
|
||||
pub use object::Citation;
|
||||
pub use object::CitationReference;
|
||||
pub use object::Code;
|
||||
pub use object::Entity;
|
||||
pub use object::ExportSnippet;
|
||||
pub use object::FootnoteReference;
|
||||
pub use object::InlineBabelCall;
|
||||
pub use object::InlineSourceBlock;
|
||||
pub use object::Italic;
|
||||
pub use object::LatexFragment;
|
||||
pub use object::LineBreak;
|
||||
pub use object::Object;
|
||||
pub use object::OrgMacro;
|
||||
pub use object::PlainLink;
|
||||
pub use object::PlainText;
|
||||
pub use object::RadioLink;
|
||||
pub use object::RadioTarget;
|
||||
pub use object::RegularLink;
|
||||
pub use object::StatisticsCookie;
|
||||
pub use object::StrikeThrough;
|
||||
pub use object::Subscript;
|
||||
pub use object::Superscript;
|
||||
pub use object::Target;
|
||||
pub use object::Timestamp;
|
||||
pub use object::Underline;
|
||||
pub use object::Verbatim;
|
||||
pub use source::Source;
|
||||
type Context<'r, 's> = &'r parser_context::ContextTree<'r, 's>;
|
||||
pub use document::parse;
|
||||
pub use document::parse_with_settings;
|
||||
pub use org_source::OrgSource;
|
||||
|
||||
@@ -2,10 +2,13 @@ use nom::branch::alt;
|
||||
use nom::combinator::map;
|
||||
|
||||
use super::org_source::OrgSource;
|
||||
use super::parser_with_context::parser_with_context;
|
||||
use super::plain_text::plain_text;
|
||||
use super::regular_link::regular_link;
|
||||
use super::Context;
|
||||
use super::subscript_and_superscript::detect_subscript_or_superscript;
|
||||
use crate::context::parser_with_context;
|
||||
use crate::context::RefContext;
|
||||
use crate::error::CustomError;
|
||||
use crate::error::MyError;
|
||||
use crate::error::Res;
|
||||
use crate::parser::angle_link::angle_link;
|
||||
use crate::parser::citation::citation;
|
||||
@@ -16,7 +19,6 @@ use crate::parser::inline_babel_call::inline_babel_call;
|
||||
use crate::parser::inline_source_block::inline_source_block;
|
||||
use crate::parser::latex_fragment::latex_fragment;
|
||||
use crate::parser::line_break::line_break;
|
||||
use crate::parser::object::Object;
|
||||
use crate::parser::org_macro::org_macro;
|
||||
use crate::parser::plain_link::plain_link;
|
||||
use crate::parser::radio_link::radio_link;
|
||||
@@ -27,90 +29,41 @@ use crate::parser::subscript_and_superscript::superscript;
|
||||
use crate::parser::target::target;
|
||||
use crate::parser::text_markup::text_markup;
|
||||
use crate::parser::timestamp::timestamp;
|
||||
use crate::types::Object;
|
||||
|
||||
#[cfg_attr(feature = "tracing", tracing::instrument(ret, level = "debug"))]
|
||||
pub fn standard_set_object<'r, 's>(
|
||||
context: Context<'r, 's>,
|
||||
pub fn standard_set_object<'b, 'g, 'r, 's>(
|
||||
context: RefContext<'b, 'g, 'r, 's>,
|
||||
input: OrgSource<'s>,
|
||||
) -> Res<OrgSource<'s>, Object<'s>> {
|
||||
let (remaining, object) = alt((
|
||||
map(parser_with_context!(timestamp)(context), Object::Timestamp),
|
||||
map(parser_with_context!(subscript)(context), Object::Subscript),
|
||||
parser_with_context!(standard_set_object_sans_plain_text)(context),
|
||||
map(
|
||||
parser_with_context!(superscript)(context),
|
||||
Object::Superscript,
|
||||
parser_with_context!(plain_text(detect_standard_set_object_sans_plain_text))(context),
|
||||
Object::PlainText,
|
||||
),
|
||||
map(
|
||||
parser_with_context!(statistics_cookie)(context),
|
||||
Object::StatisticsCookie,
|
||||
),
|
||||
map(parser_with_context!(target)(context), Object::Target),
|
||||
map(parser_with_context!(line_break)(context), Object::LineBreak),
|
||||
map(
|
||||
parser_with_context!(inline_source_block)(context),
|
||||
Object::InlineSourceBlock,
|
||||
),
|
||||
map(
|
||||
parser_with_context!(inline_babel_call)(context),
|
||||
Object::InlineBabelCall,
|
||||
),
|
||||
map(parser_with_context!(citation)(context), Object::Citation),
|
||||
map(
|
||||
parser_with_context!(footnote_reference)(context),
|
||||
Object::FootnoteReference,
|
||||
),
|
||||
map(
|
||||
parser_with_context!(export_snippet)(context),
|
||||
Object::ExportSnippet,
|
||||
),
|
||||
map(parser_with_context!(entity)(context), Object::Entity),
|
||||
map(
|
||||
parser_with_context!(latex_fragment)(context),
|
||||
Object::LatexFragment,
|
||||
),
|
||||
map(parser_with_context!(radio_link)(context), Object::RadioLink),
|
||||
map(
|
||||
parser_with_context!(radio_target)(context),
|
||||
Object::RadioTarget,
|
||||
),
|
||||
parser_with_context!(text_markup)(context),
|
||||
map(
|
||||
parser_with_context!(regular_link)(context),
|
||||
Object::RegularLink,
|
||||
),
|
||||
map(parser_with_context!(plain_link)(context), Object::PlainLink),
|
||||
map(parser_with_context!(angle_link)(context), Object::AngleLink),
|
||||
map(parser_with_context!(org_macro)(context), Object::OrgMacro),
|
||||
map(parser_with_context!(plain_text)(context), Object::PlainText),
|
||||
))(input)?;
|
||||
Ok((remaining, object))
|
||||
}
|
||||
|
||||
#[cfg_attr(feature = "tracing", tracing::instrument(ret, level = "debug"))]
|
||||
pub fn minimal_set_object<'r, 's>(
|
||||
context: Context<'r, 's>,
|
||||
pub fn minimal_set_object<'b, 'g, 'r, 's>(
|
||||
context: RefContext<'b, 'g, 'r, 's>,
|
||||
input: OrgSource<'s>,
|
||||
) -> Res<OrgSource<'s>, Object<'s>> {
|
||||
let (remaining, object) = alt((
|
||||
map(parser_with_context!(subscript)(context), Object::Subscript),
|
||||
parser_with_context!(minimal_set_object_sans_plain_text)(context),
|
||||
map(
|
||||
parser_with_context!(superscript)(context),
|
||||
Object::Superscript,
|
||||
parser_with_context!(plain_text(detect_minimal_set_object_sans_plain_text))(context),
|
||||
Object::PlainText,
|
||||
),
|
||||
map(parser_with_context!(entity)(context), Object::Entity),
|
||||
map(
|
||||
parser_with_context!(latex_fragment)(context),
|
||||
Object::LatexFragment,
|
||||
),
|
||||
parser_with_context!(text_markup)(context),
|
||||
map(parser_with_context!(plain_text)(context), Object::PlainText),
|
||||
))(input)?;
|
||||
Ok((remaining, object))
|
||||
}
|
||||
|
||||
#[cfg_attr(feature = "tracing", tracing::instrument(ret, level = "debug"))]
|
||||
pub fn any_object_except_plain_text<'r, 's>(
|
||||
context: Context<'r, 's>,
|
||||
fn standard_set_object_sans_plain_text<'b, 'g, 'r, 's>(
|
||||
context: RefContext<'b, 'g, 'r, 's>,
|
||||
input: OrgSource<'s>,
|
||||
) -> Res<OrgSource<'s>, Object<'s>> {
|
||||
let (remaining, object) = alt((
|
||||
@@ -166,8 +119,81 @@ pub fn any_object_except_plain_text<'r, 's>(
|
||||
}
|
||||
|
||||
#[cfg_attr(feature = "tracing", tracing::instrument(ret, level = "debug"))]
|
||||
pub fn regular_link_description_object_set<'r, 's>(
|
||||
context: Context<'r, 's>,
|
||||
fn minimal_set_object_sans_plain_text<'b, 'g, 'r, 's>(
|
||||
context: RefContext<'b, 'g, 'r, 's>,
|
||||
input: OrgSource<'s>,
|
||||
) -> Res<OrgSource<'s>, Object<'s>> {
|
||||
let (remaining, object) = alt((
|
||||
map(parser_with_context!(subscript)(context), Object::Subscript),
|
||||
map(
|
||||
parser_with_context!(superscript)(context),
|
||||
Object::Superscript,
|
||||
),
|
||||
map(parser_with_context!(entity)(context), Object::Entity),
|
||||
map(
|
||||
parser_with_context!(latex_fragment)(context),
|
||||
Object::LatexFragment,
|
||||
),
|
||||
parser_with_context!(text_markup)(context),
|
||||
))(input)?;
|
||||
Ok((remaining, object))
|
||||
}
|
||||
|
||||
#[cfg_attr(feature = "tracing", tracing::instrument(ret, level = "debug"))]
|
||||
pub fn detect_standard_set_object_sans_plain_text<'b, 'g, 'r, 's>(
|
||||
context: RefContext<'b, 'g, 'r, 's>,
|
||||
input: OrgSource<'s>,
|
||||
) -> Res<OrgSource<'s>, ()> {
|
||||
if detect_subscript_or_superscript(input).is_ok() {
|
||||
return Ok((input, ()));
|
||||
}
|
||||
if standard_set_object_sans_plain_text(context, input).is_ok() {
|
||||
return Ok((input, ()));
|
||||
}
|
||||
|
||||
return Err(nom::Err::Error(CustomError::MyError(MyError(
|
||||
"No object detected.".into(),
|
||||
))));
|
||||
}
|
||||
|
||||
#[cfg_attr(feature = "tracing", tracing::instrument(ret, level = "debug"))]
|
||||
fn detect_minimal_set_object_sans_plain_text<'b, 'g, 'r, 's>(
|
||||
context: RefContext<'b, 'g, 'r, 's>,
|
||||
input: OrgSource<'s>,
|
||||
) -> Res<OrgSource<'s>, ()> {
|
||||
if detect_subscript_or_superscript(input).is_ok() {
|
||||
return Ok((input, ()));
|
||||
}
|
||||
if minimal_set_object_sans_plain_text(context, input).is_ok() {
|
||||
return Ok((input, ()));
|
||||
}
|
||||
|
||||
return Err(nom::Err::Error(CustomError::MyError(MyError(
|
||||
"No object detected.".into(),
|
||||
))));
|
||||
}
|
||||
|
||||
#[cfg_attr(feature = "tracing", tracing::instrument(ret, level = "debug"))]
|
||||
pub fn regular_link_description_set_object<'b, 'g, 'r, 's>(
|
||||
context: RefContext<'b, 'g, 'r, 's>,
|
||||
input: OrgSource<'s>,
|
||||
) -> Res<OrgSource<'s>, Object<'s>> {
|
||||
// TODO: It can also contain another link, but only when it is a plain or angle link. It can contain square brackets, but not ]]
|
||||
let (remaining, object) = alt((
|
||||
parser_with_context!(regular_link_description_set_object_sans_plain_text)(context),
|
||||
map(
|
||||
parser_with_context!(plain_text(
|
||||
detect_regular_link_description_set_object_sans_plain_text
|
||||
))(context),
|
||||
Object::PlainText,
|
||||
),
|
||||
))(input)?;
|
||||
Ok((remaining, object))
|
||||
}
|
||||
|
||||
#[cfg_attr(feature = "tracing", tracing::instrument(ret, level = "debug"))]
|
||||
fn regular_link_description_set_object_sans_plain_text<'b, 'g, 'r, 's>(
|
||||
context: RefContext<'b, 'g, 'r, 's>,
|
||||
input: OrgSource<'s>,
|
||||
) -> Res<OrgSource<'s>, Object<'s>> {
|
||||
// TODO: It can also contain another link, but only when it is a plain or angle link. It can contain square brackets, but not ]]
|
||||
@@ -189,14 +215,46 @@ pub fn regular_link_description_object_set<'r, 's>(
|
||||
Object::InlineBabelCall,
|
||||
),
|
||||
map(parser_with_context!(org_macro)(context), Object::OrgMacro),
|
||||
parser_with_context!(minimal_set_object)(context),
|
||||
parser_with_context!(minimal_set_object_sans_plain_text)(context),
|
||||
))(input)?;
|
||||
Ok((remaining, object))
|
||||
}
|
||||
|
||||
#[cfg_attr(feature = "tracing", tracing::instrument(ret, level = "debug"))]
|
||||
pub fn table_cell_set_object<'r, 's>(
|
||||
context: Context<'r, 's>,
|
||||
pub fn detect_regular_link_description_set_object_sans_plain_text<'b, 'g, 'r, 's>(
|
||||
context: RefContext<'b, 'g, 'r, 's>,
|
||||
input: OrgSource<'s>,
|
||||
) -> Res<OrgSource<'s>, ()> {
|
||||
if detect_subscript_or_superscript(input).is_ok() {
|
||||
return Ok((input, ()));
|
||||
}
|
||||
if regular_link_description_set_object_sans_plain_text(context, input).is_ok() {
|
||||
return Ok((input, ()));
|
||||
}
|
||||
|
||||
return Err(nom::Err::Error(CustomError::MyError(MyError(
|
||||
"No object detected.".into(),
|
||||
))));
|
||||
}
|
||||
|
||||
#[cfg_attr(feature = "tracing", tracing::instrument(ret, level = "debug"))]
|
||||
pub fn table_cell_set_object<'b, 'g, 'r, 's>(
|
||||
context: RefContext<'b, 'g, 'r, 's>,
|
||||
input: OrgSource<'s>,
|
||||
) -> Res<OrgSource<'s>, Object<'s>> {
|
||||
let (remaining, object) = alt((
|
||||
parser_with_context!(table_cell_set_object_sans_plain_text)(context),
|
||||
map(
|
||||
parser_with_context!(plain_text(detect_table_cell_set_object_sans_plain_text))(context),
|
||||
Object::PlainText,
|
||||
),
|
||||
))(input)?;
|
||||
Ok((remaining, object))
|
||||
}
|
||||
|
||||
#[cfg_attr(feature = "tracing", tracing::instrument(ret, level = "debug"))]
|
||||
pub fn table_cell_set_object_sans_plain_text<'b, 'g, 'r, 's>(
|
||||
context: RefContext<'b, 'g, 'r, 's>,
|
||||
input: OrgSource<'s>,
|
||||
) -> Res<OrgSource<'s>, Object<'s>> {
|
||||
let (remaining, object) = alt((
|
||||
@@ -223,7 +281,24 @@ pub fn table_cell_set_object<'r, 's>(
|
||||
),
|
||||
map(parser_with_context!(target)(context), Object::Target),
|
||||
map(parser_with_context!(timestamp)(context), Object::Timestamp),
|
||||
parser_with_context!(minimal_set_object)(context),
|
||||
parser_with_context!(minimal_set_object_sans_plain_text)(context),
|
||||
))(input)?;
|
||||
Ok((remaining, object))
|
||||
}
|
||||
|
||||
#[cfg_attr(feature = "tracing", tracing::instrument(ret, level = "debug"))]
|
||||
pub fn detect_table_cell_set_object_sans_plain_text<'b, 'g, 'r, 's>(
|
||||
context: RefContext<'b, 'g, 'r, 's>,
|
||||
input: OrgSource<'s>,
|
||||
) -> Res<OrgSource<'s>, ()> {
|
||||
if detect_subscript_or_superscript(input).is_ok() {
|
||||
return Ok((input, ()));
|
||||
}
|
||||
if table_cell_set_object_sans_plain_text(context, input).is_ok() {
|
||||
return Ok((input, ()));
|
||||
}
|
||||
|
||||
return Err(nom::Err::Error(CustomError::MyError(MyError(
|
||||
"No object detected.".into(),
|
||||
))));
|
||||
}
|
||||
|
||||
@@ -9,17 +9,17 @@ use nom::multi::separated_list0;
|
||||
|
||||
use super::org_source::OrgSource;
|
||||
use super::util::maybe_consume_object_trailing_whitespace_if_not_exiting;
|
||||
use super::Context;
|
||||
use crate::context::parser_with_context;
|
||||
use crate::context::RefContext;
|
||||
use crate::error::CustomError;
|
||||
use crate::error::Res;
|
||||
use crate::parser::object::OrgMacro;
|
||||
use crate::parser::parser_with_context::parser_with_context;
|
||||
use crate::parser::util::exit_matcher_parser;
|
||||
use crate::parser::util::get_consumed;
|
||||
use crate::types::OrgMacro;
|
||||
|
||||
#[cfg_attr(feature = "tracing", tracing::instrument(ret, level = "debug"))]
|
||||
pub fn org_macro<'r, 's>(
|
||||
context: Context<'r, 's>,
|
||||
pub fn org_macro<'b, 'g, 'r, 's>(
|
||||
context: RefContext<'b, 'g, 'r, 's>,
|
||||
input: OrgSource<'s>,
|
||||
) -> Res<OrgSource<'s>, OrgMacro<'s>> {
|
||||
let (remaining, _) = tag("{{{")(input)?;
|
||||
@@ -45,8 +45,8 @@ pub fn org_macro<'r, 's>(
|
||||
}
|
||||
|
||||
#[cfg_attr(feature = "tracing", tracing::instrument(ret, level = "debug"))]
|
||||
fn org_macro_name<'r, 's>(
|
||||
_context: Context<'r, 's>,
|
||||
fn org_macro_name<'b, 'g, 'r, 's>(
|
||||
_context: RefContext<'b, 'g, 'r, 's>,
|
||||
input: OrgSource<'s>,
|
||||
) -> Res<OrgSource<'s>, OrgSource<'s>> {
|
||||
let (remaining, _) = verify(anychar, |c| c.is_alphabetic())(input)?;
|
||||
@@ -58,8 +58,8 @@ fn org_macro_name<'r, 's>(
|
||||
}
|
||||
|
||||
#[cfg_attr(feature = "tracing", tracing::instrument(ret, level = "debug"))]
|
||||
fn org_macro_args<'r, 's>(
|
||||
context: Context<'r, 's>,
|
||||
fn org_macro_args<'b, 'g, 'r, 's>(
|
||||
context: RefContext<'b, 'g, 'r, 's>,
|
||||
input: OrgSource<'s>,
|
||||
) -> Res<OrgSource<'s>, Vec<OrgSource<'s>>> {
|
||||
let (remaining, _) = tag("(")(input)?;
|
||||
@@ -71,8 +71,8 @@ fn org_macro_args<'r, 's>(
|
||||
}
|
||||
|
||||
#[cfg_attr(feature = "tracing", tracing::instrument(ret, level = "debug"))]
|
||||
fn org_macro_arg<'r, 's>(
|
||||
context: Context<'r, 's>,
|
||||
fn org_macro_arg<'b, 'g, 'r, 's>(
|
||||
context: RefContext<'b, 'g, 'r, 's>,
|
||||
input: OrgSource<'s>,
|
||||
) -> Res<OrgSource<'s>, OrgSource<'s>> {
|
||||
let mut remaining = input;
|
||||
|
||||
@@ -27,7 +27,7 @@ pub struct OrgSource<'s> {
|
||||
|
||||
impl<'s> std::fmt::Debug for OrgSource<'s> {
|
||||
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
||||
f.debug_tuple("OrgSource")
|
||||
f.debug_tuple("Org")
|
||||
.field(&Into::<&str>::into(self))
|
||||
.finish()
|
||||
}
|
||||
@@ -145,6 +145,9 @@ where
|
||||
if new_end > self.end {
|
||||
panic!("Attempted to extend past the end of the WrappedInput.")
|
||||
}
|
||||
if new_start == self.start && new_end == self.end {
|
||||
return self.clone();
|
||||
}
|
||||
|
||||
let skipped_text = &self.full_source[self.start..new_start];
|
||||
let mut start_of_line = self.start_of_line;
|
||||
@@ -183,7 +186,7 @@ where
|
||||
start: new_start,
|
||||
end: new_end,
|
||||
start_of_line,
|
||||
preceding_character: skipped_text.chars().last(),
|
||||
preceding_character: skipped_text.chars().last().or(self.preceding_character),
|
||||
bracket_depth,
|
||||
brace_depth,
|
||||
parenthesis_depth,
|
||||
@@ -318,6 +321,7 @@ impl<'s> From<CustomError<OrgSource<'s>>> for CustomError<&'s str> {
|
||||
match value {
|
||||
CustomError::MyError(err) => CustomError::MyError(err.into()),
|
||||
CustomError::Nom(input, error_kind) => CustomError::Nom(input.into(), error_kind),
|
||||
CustomError::IO(err) => CustomError::IO(err),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -7,30 +7,30 @@ use nom::multi::many_till;
|
||||
use nom::sequence::tuple;
|
||||
|
||||
use super::element_parser::detect_element;
|
||||
use super::lesser_element::Paragraph;
|
||||
use super::org_source::OrgSource;
|
||||
use super::util::blank_line;
|
||||
use super::util::get_consumed;
|
||||
use super::Context;
|
||||
use crate::context::parser_with_context;
|
||||
use crate::context::ContextElement;
|
||||
use crate::context::ExitClass;
|
||||
use crate::context::ExitMatcherNode;
|
||||
use crate::context::RefContext;
|
||||
use crate::error::Res;
|
||||
use crate::parser::exiting::ExitClass;
|
||||
use crate::parser::object_parser::standard_set_object;
|
||||
use crate::parser::parser_context::ContextElement;
|
||||
use crate::parser::parser_context::ExitMatcherNode;
|
||||
use crate::parser::parser_with_context::parser_with_context;
|
||||
use crate::parser::util::exit_matcher_parser;
|
||||
use crate::parser::util::start_of_line;
|
||||
use crate::types::Paragraph;
|
||||
|
||||
#[cfg_attr(feature = "tracing", tracing::instrument(ret, level = "debug"))]
|
||||
pub fn paragraph<'r, 's>(
|
||||
context: Context<'r, 's>,
|
||||
pub fn paragraph<'b, 'g, 'r, 's>(
|
||||
context: RefContext<'b, 'g, 'r, 's>,
|
||||
input: OrgSource<'s>,
|
||||
) -> Res<OrgSource<'s>, Paragraph<'s>> {
|
||||
let parser_context =
|
||||
context.with_additional_node(ContextElement::ExitMatcherNode(ExitMatcherNode {
|
||||
class: ExitClass::Gamma,
|
||||
exit_matcher: ¶graph_end,
|
||||
}));
|
||||
let parser_context = ContextElement::ExitMatcherNode(ExitMatcherNode {
|
||||
class: ExitClass::Gamma,
|
||||
exit_matcher: ¶graph_end,
|
||||
});
|
||||
let parser_context = context.with_additional_node(&parser_context);
|
||||
let standard_set_object_matcher = parser_with_context!(standard_set_object)(&parser_context);
|
||||
let exit_matcher = parser_with_context!(exit_matcher_parser)(&parser_context);
|
||||
|
||||
@@ -53,8 +53,8 @@ pub fn paragraph<'r, 's>(
|
||||
}
|
||||
|
||||
#[cfg_attr(feature = "tracing", tracing::instrument(ret, level = "debug"))]
|
||||
fn paragraph_end<'r, 's>(
|
||||
context: Context<'r, 's>,
|
||||
fn paragraph_end<'b, 'g, 'r, 's>(
|
||||
context: RefContext<'b, 'g, 'r, 's>,
|
||||
input: OrgSource<'s>,
|
||||
) -> Res<OrgSource<'s>, OrgSource<'s>> {
|
||||
let non_paragraph_element_matcher = parser_with_context!(detect_element(false))(context);
|
||||
@@ -67,16 +67,21 @@ fn paragraph_end<'r, 's>(
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use crate::context::parser_with_context;
|
||||
use crate::context::Context;
|
||||
use crate::context::ContextElement;
|
||||
use crate::context::GlobalSettings;
|
||||
use crate::context::List;
|
||||
use crate::parser::element_parser::element;
|
||||
use crate::parser::org_source::OrgSource;
|
||||
use crate::parser::parser_context::ContextTree;
|
||||
use crate::parser::parser_with_context::parser_with_context;
|
||||
use crate::parser::source::Source;
|
||||
use crate::types::Source;
|
||||
|
||||
#[test]
|
||||
fn two_paragraphs() {
|
||||
let input = OrgSource::new("foo bar baz\n\nlorem ipsum");
|
||||
let initial_context: ContextTree<'_, '_> = ContextTree::new();
|
||||
let global_settings = GlobalSettings::default();
|
||||
let initial_context = ContextElement::document_context();
|
||||
let initial_context = Context::new(&global_settings, List::new(&initial_context));
|
||||
let paragraph_matcher = parser_with_context!(element(true))(&initial_context);
|
||||
let (remaining, first_paragraph) = paragraph_matcher(input).expect("Parse first paragraph");
|
||||
let (remaining, second_paragraph) =
|
||||
|
||||
@@ -1,139 +0,0 @@
|
||||
use std::rc::Rc;
|
||||
|
||||
use nom::combinator::eof;
|
||||
use nom::IResult;
|
||||
|
||||
use super::list::List;
|
||||
use super::list::Node;
|
||||
use super::org_source::OrgSource;
|
||||
use super::Context;
|
||||
use super::Object;
|
||||
use crate::error::CustomError;
|
||||
use crate::error::MyError;
|
||||
use crate::error::Res;
|
||||
use crate::parser::exiting::ExitClass;
|
||||
|
||||
type Matcher =
|
||||
dyn for<'r, 's> Fn(Context<'r, 's>, OrgSource<'s>) -> Res<OrgSource<'s>, OrgSource<'s>>;
|
||||
|
||||
#[derive(Debug, Clone)]
|
||||
pub struct ContextTree<'r, 's> {
|
||||
tree: List<ContextElement<'r, 's>>,
|
||||
}
|
||||
|
||||
impl<'r, 's> ContextTree<'r, 's> {
|
||||
pub fn new() -> Self {
|
||||
ContextTree { tree: List::new() }
|
||||
}
|
||||
|
||||
pub fn branch_from(trunk: &Rc<Node<ContextElement<'r, 's>>>) -> Self {
|
||||
ContextTree {
|
||||
tree: List::branch_from(trunk),
|
||||
}
|
||||
}
|
||||
|
||||
#[allow(dead_code)]
|
||||
pub fn ptr_eq<'x, 'y>(&self, other: &ContextTree<'x, 'y>) -> bool {
|
||||
self.tree.ptr_eq(&other.tree)
|
||||
}
|
||||
|
||||
pub fn with_additional_node(&self, data: ContextElement<'r, 's>) -> ContextTree<'r, 's> {
|
||||
let new_list = self.tree.push_front(data);
|
||||
ContextTree { tree: new_list }
|
||||
}
|
||||
|
||||
pub fn iter(&self) -> impl Iterator<Item = &Rc<Node<ContextElement<'r, 's>>>> {
|
||||
self.tree.iter()
|
||||
}
|
||||
|
||||
#[cfg_attr(feature = "tracing", tracing::instrument(ret, level = "debug"))]
|
||||
pub fn check_exit_matcher(
|
||||
&'r self,
|
||||
i: OrgSource<'s>,
|
||||
) -> IResult<OrgSource<'s>, OrgSource<'s>, CustomError<OrgSource<'s>>> {
|
||||
// Special check for EOF. We don't just make this a document-level exit matcher since the IgnoreParent ChainBehavior could cause early exit matchers to not run.
|
||||
let at_end_of_file = eof(i);
|
||||
if at_end_of_file.is_ok() {
|
||||
return at_end_of_file;
|
||||
}
|
||||
|
||||
// let blocked_context =
|
||||
// self.with_additional_node(ContextElement::ExitMatcherNode(ExitMatcherNode {
|
||||
// exit_matcher: ChainBehavior::IgnoreParent(Some(&always_fail)),
|
||||
// }));
|
||||
|
||||
let mut current_class_filter = ExitClass::Gamma;
|
||||
for current_node in self.iter() {
|
||||
let context_element = current_node.get_data();
|
||||
match context_element {
|
||||
ContextElement::ExitMatcherNode(exit_matcher) => {
|
||||
if exit_matcher.class as u32 <= current_class_filter as u32 {
|
||||
current_class_filter = exit_matcher.class;
|
||||
let local_context = ContextTree::branch_from(current_node);
|
||||
let local_result = (exit_matcher.exit_matcher)(&local_context, i);
|
||||
if local_result.is_ok() {
|
||||
return local_result;
|
||||
}
|
||||
}
|
||||
}
|
||||
_ => {}
|
||||
};
|
||||
}
|
||||
// TODO: Make this a specific error instead of just a generic MyError
|
||||
return Err(nom::Err::Error(CustomError::MyError(MyError(
|
||||
"NoExit".into(),
|
||||
))));
|
||||
}
|
||||
|
||||
/// Indicates if elements should consume the whitespace after them.
|
||||
///
|
||||
/// Defaults to true.
|
||||
pub fn should_consume_trailing_whitespace(&self) -> bool {
|
||||
self._should_consume_trailing_whitespace().unwrap_or(true)
|
||||
}
|
||||
|
||||
fn _should_consume_trailing_whitespace(&self) -> Option<bool> {
|
||||
for current_node in self.iter() {
|
||||
let context_element = current_node.get_data();
|
||||
match context_element {
|
||||
ContextElement::ConsumeTrailingWhitespace(should) => {
|
||||
return Some(*should);
|
||||
}
|
||||
_ => {}
|
||||
}
|
||||
}
|
||||
None
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug)]
|
||||
pub enum ContextElement<'r, 's> {
|
||||
/// Stores a parser that indicates that children should exit upon matching an exit matcher.
|
||||
ExitMatcherNode(ExitMatcherNode<'r>),
|
||||
|
||||
/// Stores the name of the current element to prevent directly nesting elements of the same type.
|
||||
Context(&'r str),
|
||||
|
||||
/// Indicates if elements should consume the whitespace after them.
|
||||
ConsumeTrailingWhitespace(bool),
|
||||
|
||||
/// The contents of a radio target.
|
||||
///
|
||||
/// If any are found, this will force a 2nd parse through the
|
||||
/// org-mode document since text needs to be re-parsed to look for
|
||||
/// radio links matching the contents of radio targets.
|
||||
RadioTarget(Vec<&'r Vec<Object<'s>>>),
|
||||
}
|
||||
|
||||
pub struct ExitMatcherNode<'r> {
|
||||
pub exit_matcher: &'r Matcher,
|
||||
pub class: ExitClass,
|
||||
}
|
||||
|
||||
impl<'r> std::fmt::Debug for ExitMatcherNode<'r> {
|
||||
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
||||
let mut formatter = f.debug_struct("ExitMatcherNode");
|
||||
formatter.field("class", &self.class.to_string());
|
||||
formatter.finish()
|
||||
}
|
||||
}
|
||||
@@ -11,18 +11,19 @@ use nom::combinator::verify;
|
||||
use nom::multi::many_till;
|
||||
|
||||
use super::org_source::OrgSource;
|
||||
use super::Context;
|
||||
use super::util::maybe_consume_object_trailing_whitespace_if_not_exiting;
|
||||
use crate::context::parser_with_context;
|
||||
use crate::context::ContextElement;
|
||||
use crate::context::ExitClass;
|
||||
use crate::context::ExitMatcherNode;
|
||||
use crate::context::RefContext;
|
||||
use crate::error::CustomError;
|
||||
use crate::error::MyError;
|
||||
use crate::error::Res;
|
||||
use crate::parser::exiting::ExitClass;
|
||||
use crate::parser::object::PlainLink;
|
||||
use crate::parser::parser_context::ContextElement;
|
||||
use crate::parser::parser_context::ExitMatcherNode;
|
||||
use crate::parser::parser_with_context::parser_with_context;
|
||||
use crate::parser::util::exit_matcher_parser;
|
||||
use crate::parser::util::get_consumed;
|
||||
use crate::parser::util::WORD_CONSTITUENT_CHARACTERS;
|
||||
use crate::types::PlainLink;
|
||||
|
||||
// TODO: Make this a user-provided variable corresponding to elisp's org-link-parameters
|
||||
const ORG_LINK_PARAMETERS: [&'static str; 23] = [
|
||||
@@ -52,8 +53,8 @@ const ORG_LINK_PARAMETERS: [&'static str; 23] = [
|
||||
];
|
||||
|
||||
#[cfg_attr(feature = "tracing", tracing::instrument(ret, level = "debug"))]
|
||||
pub fn plain_link<'r, 's>(
|
||||
context: Context<'r, 's>,
|
||||
pub fn plain_link<'b, 'g, 'r, 's>(
|
||||
context: RefContext<'b, 'g, 'r, 's>,
|
||||
input: OrgSource<'s>,
|
||||
) -> Res<OrgSource<'s>, PlainLink<'s>> {
|
||||
let (remaining, _) = pre(context, input)?;
|
||||
@@ -61,6 +62,8 @@ pub fn plain_link<'r, 's>(
|
||||
let (remaining, _separator) = tag(":")(remaining)?;
|
||||
let (remaining, path) = path_plain(context, remaining)?;
|
||||
peek(parser_with_context!(post)(context))(remaining)?;
|
||||
let (remaining, _trailing_whitespace) =
|
||||
maybe_consume_object_trailing_whitespace_if_not_exiting(context, remaining)?;
|
||||
let source = get_consumed(input, remaining);
|
||||
Ok((
|
||||
remaining,
|
||||
@@ -73,7 +76,10 @@ pub fn plain_link<'r, 's>(
|
||||
}
|
||||
|
||||
#[cfg_attr(feature = "tracing", tracing::instrument(ret, level = "debug"))]
|
||||
fn pre<'r, 's>(_context: Context<'r, 's>, input: OrgSource<'s>) -> Res<OrgSource<'s>, ()> {
|
||||
fn pre<'b, 'g, 'r, 's>(
|
||||
_context: RefContext<'b, 'g, 'r, 's>,
|
||||
input: OrgSource<'s>,
|
||||
) -> Res<OrgSource<'s>, ()> {
|
||||
let preceding_character = input.get_preceding_character();
|
||||
match preceding_character {
|
||||
// If None, we are at the start of the file which is fine
|
||||
@@ -90,14 +96,17 @@ fn pre<'r, 's>(_context: Context<'r, 's>, input: OrgSource<'s>) -> Res<OrgSource
|
||||
}
|
||||
|
||||
#[cfg_attr(feature = "tracing", tracing::instrument(ret, level = "debug"))]
|
||||
fn post<'r, 's>(_context: Context<'r, 's>, input: OrgSource<'s>) -> Res<OrgSource<'s>, ()> {
|
||||
fn post<'b, 'g, 'r, 's>(
|
||||
_context: RefContext<'b, 'g, 'r, 's>,
|
||||
input: OrgSource<'s>,
|
||||
) -> Res<OrgSource<'s>, ()> {
|
||||
let (remaining, _) = alt((eof, recognize(none_of(WORD_CONSTITUENT_CHARACTERS))))(input)?;
|
||||
Ok((remaining, ()))
|
||||
}
|
||||
|
||||
#[cfg_attr(feature = "tracing", tracing::instrument(ret, level = "debug"))]
|
||||
pub fn protocol<'r, 's>(
|
||||
_context: Context<'r, 's>,
|
||||
pub fn protocol<'b, 'g, 'r, 's>(
|
||||
_context: RefContext<'b, 'g, 'r, 's>,
|
||||
input: OrgSource<'s>,
|
||||
) -> Res<OrgSource<'s>, OrgSource<'s>> {
|
||||
// TODO: This should be defined by org-link-parameters
|
||||
@@ -117,16 +126,16 @@ pub fn protocol<'r, 's>(
|
||||
}
|
||||
|
||||
#[cfg_attr(feature = "tracing", tracing::instrument(ret, level = "debug"))]
|
||||
fn path_plain<'r, 's>(
|
||||
context: Context<'r, 's>,
|
||||
fn path_plain<'b, 'g, 'r, 's>(
|
||||
context: RefContext<'b, 'g, 'r, 's>,
|
||||
input: OrgSource<'s>,
|
||||
) -> Res<OrgSource<'s>, OrgSource<'s>> {
|
||||
// TODO: "optionally containing parenthesis-wrapped non-whitespace non-bracket substrings up to a depth of two. The string must end with either a non-punctation non-whitespace character, a forwards slash, or a parenthesis-wrapped substring"
|
||||
let parser_context =
|
||||
context.with_additional_node(ContextElement::ExitMatcherNode(ExitMatcherNode {
|
||||
class: ExitClass::Gamma,
|
||||
exit_matcher: &path_plain_end,
|
||||
}));
|
||||
let parser_context = ContextElement::ExitMatcherNode(ExitMatcherNode {
|
||||
class: ExitClass::Gamma,
|
||||
exit_matcher: &path_plain_end,
|
||||
});
|
||||
let parser_context = context.with_additional_node(&parser_context);
|
||||
|
||||
let exit_matcher = parser_with_context!(exit_matcher_parser)(&parser_context);
|
||||
|
||||
@@ -139,8 +148,8 @@ fn path_plain<'r, 's>(
|
||||
}
|
||||
|
||||
#[cfg_attr(feature = "tracing", tracing::instrument(ret, level = "debug"))]
|
||||
fn path_plain_end<'r, 's>(
|
||||
_context: Context<'r, 's>,
|
||||
fn path_plain_end<'b, 'g, 'r, 's>(
|
||||
_context: RefContext<'b, 'g, 'r, 's>,
|
||||
input: OrgSource<'s>,
|
||||
) -> Res<OrgSource<'s>, OrgSource<'s>> {
|
||||
recognize(many_till(
|
||||
|
||||
@@ -16,26 +16,28 @@ use nom::multi::many1;
|
||||
use nom::multi::many_till;
|
||||
use nom::sequence::tuple;
|
||||
|
||||
use super::greater_element::PlainList;
|
||||
use super::greater_element::PlainListItem;
|
||||
use super::element_parser::element;
|
||||
use super::object_parser::standard_set_object;
|
||||
use super::org_source::OrgSource;
|
||||
use super::parser_with_context::parser_with_context;
|
||||
use super::util::include_input;
|
||||
use super::util::non_whitespace_character;
|
||||
use super::Context;
|
||||
use super::Object;
|
||||
use crate::context::parser_with_context;
|
||||
use crate::context::ContextElement;
|
||||
use crate::context::ContextMatcher;
|
||||
use crate::context::ExitClass;
|
||||
use crate::context::ExitMatcherNode;
|
||||
use crate::context::RefContext;
|
||||
use crate::error::CustomError;
|
||||
use crate::error::MyError;
|
||||
use crate::error::Res;
|
||||
use crate::parser::element_parser::element;
|
||||
use crate::parser::exiting::ExitClass;
|
||||
use crate::parser::parser_context::ContextElement;
|
||||
use crate::parser::parser_context::ExitMatcherNode;
|
||||
use crate::parser::util::blank_line;
|
||||
use crate::parser::util::exit_matcher_parser;
|
||||
use crate::parser::util::get_consumed;
|
||||
use crate::parser::util::maybe_consume_trailing_whitespace_if_not_exiting;
|
||||
use crate::parser::util::start_of_line;
|
||||
use crate::types::Object;
|
||||
use crate::types::PlainList;
|
||||
use crate::types::PlainListItem;
|
||||
|
||||
#[cfg_attr(feature = "tracing", tracing::instrument(ret, level = "debug"))]
|
||||
pub fn detect_plain_list<'s>(input: OrgSource<'s>) -> Res<OrgSource<'s>, ()> {
|
||||
@@ -60,17 +62,22 @@ pub fn detect_plain_list<'s>(input: OrgSource<'s>) -> Res<OrgSource<'s>, ()> {
|
||||
}
|
||||
|
||||
#[cfg_attr(feature = "tracing", tracing::instrument(ret, level = "debug"))]
|
||||
pub fn plain_list<'r, 's>(
|
||||
context: Context<'r, 's>,
|
||||
pub fn plain_list<'b, 'g, 'r, 's>(
|
||||
context: RefContext<'b, 'g, 'r, 's>,
|
||||
input: OrgSource<'s>,
|
||||
) -> Res<OrgSource<'s>, PlainList<'s>> {
|
||||
let parser_context = context
|
||||
.with_additional_node(ContextElement::Context("plain list"))
|
||||
.with_additional_node(ContextElement::ConsumeTrailingWhitespace(true))
|
||||
.with_additional_node(ContextElement::ExitMatcherNode(ExitMatcherNode {
|
||||
let contexts = [
|
||||
ContextElement::Context("plain list"),
|
||||
ContextElement::ConsumeTrailingWhitespace(true),
|
||||
ContextElement::ExitMatcherNode(ExitMatcherNode {
|
||||
class: ExitClass::Beta,
|
||||
exit_matcher: &plain_list_end,
|
||||
}));
|
||||
}),
|
||||
];
|
||||
|
||||
let parser_context = context.with_additional_node(&contexts[0]);
|
||||
let parser_context = parser_context.with_additional_node(&contexts[1]);
|
||||
let parser_context = parser_context.with_additional_node(&contexts[2]);
|
||||
// children stores tuple of (input string, parsed object) so we can re-parse the final item
|
||||
let mut children = Vec::new();
|
||||
let mut first_item_indentation: Option<usize> = None;
|
||||
@@ -112,8 +119,8 @@ pub fn plain_list<'r, 's>(
|
||||
))));
|
||||
}
|
||||
};
|
||||
let final_item_context =
|
||||
parser_context.with_additional_node(ContextElement::ConsumeTrailingWhitespace(false));
|
||||
let final_item_context = ContextElement::ConsumeTrailingWhitespace(false);
|
||||
let final_item_context = parser_context.with_additional_node(&final_item_context);
|
||||
let (remaining, reparsed_final_item) =
|
||||
parser_with_context!(plain_list_item)(&final_item_context)(final_child_start)?;
|
||||
children.push((final_child_start, reparsed_final_item));
|
||||
@@ -129,8 +136,8 @@ pub fn plain_list<'r, 's>(
|
||||
}
|
||||
|
||||
#[cfg_attr(feature = "tracing", tracing::instrument(ret, level = "debug"))]
|
||||
pub fn plain_list_item<'r, 's>(
|
||||
context: Context<'r, 's>,
|
||||
pub fn plain_list_item<'b, 'g, 'r, 's>(
|
||||
context: RefContext<'b, 'g, 'r, 's>,
|
||||
input: OrgSource<'s>,
|
||||
) -> Res<OrgSource<'s>, PlainListItem<'s>> {
|
||||
start_of_line(input)?;
|
||||
@@ -141,13 +148,17 @@ pub fn plain_list_item<'r, 's>(
|
||||
Into::<&str>::into(bull) != "*" || indent_level > 0
|
||||
})(remaining)?;
|
||||
|
||||
let (remaining, maybe_tag) = opt(tuple((
|
||||
space1,
|
||||
parser_with_context!(item_tag)(context),
|
||||
tag(" ::"),
|
||||
)))(remaining)?;
|
||||
let maybe_contentless_item: Res<OrgSource<'_>, OrgSource<'_>> =
|
||||
peek(recognize(tuple((many0(blank_line), eof))))(remaining);
|
||||
let (remaining, _maybe_counter_set) =
|
||||
opt(tuple((space1, tag("[@"), counter, tag("]"))))(remaining)?;
|
||||
|
||||
// TODO: parse checkbox
|
||||
|
||||
let (remaining, maybe_tag) =
|
||||
opt(tuple((space1, parser_with_context!(item_tag)(context))))(remaining)?;
|
||||
|
||||
let maybe_contentless_item: Res<OrgSource<'_>, ()> = peek(parser_with_context!(
|
||||
detect_contentless_item_contents
|
||||
)(context))(remaining);
|
||||
match maybe_contentless_item {
|
||||
Ok((_rem, _ws)) => {
|
||||
let (remaining, _trailing_ws) = opt(blank_line)(remaining)?;
|
||||
@@ -159,7 +170,7 @@ pub fn plain_list_item<'r, 's>(
|
||||
indentation: indent_level,
|
||||
bullet: bull.into(),
|
||||
tag: maybe_tag
|
||||
.map(|(_ws, item_tag, _divider)| item_tag)
|
||||
.map(|(_ws, item_tag)| item_tag)
|
||||
.unwrap_or(Vec::new()),
|
||||
children: Vec::new(),
|
||||
},
|
||||
@@ -169,12 +180,15 @@ pub fn plain_list_item<'r, 's>(
|
||||
};
|
||||
let (remaining, _ws) = item_tag_post_gap(context, remaining)?;
|
||||
let exit_matcher = plain_list_item_end(indent_level);
|
||||
let parser_context = context
|
||||
.with_additional_node(ContextElement::ConsumeTrailingWhitespace(true))
|
||||
.with_additional_node(ContextElement::ExitMatcherNode(ExitMatcherNode {
|
||||
let contexts = [
|
||||
ContextElement::ConsumeTrailingWhitespace(true),
|
||||
ContextElement::ExitMatcherNode(ExitMatcherNode {
|
||||
class: ExitClass::Beta,
|
||||
exit_matcher: &exit_matcher,
|
||||
}));
|
||||
}),
|
||||
];
|
||||
let parser_context = context.with_additional_node(&contexts[0]);
|
||||
let parser_context = parser_context.with_additional_node(&contexts[1]);
|
||||
|
||||
let (mut remaining, (mut children, _exit_contents)) = many_till(
|
||||
include_input(parser_with_context!(element(true))(&parser_context)),
|
||||
@@ -182,8 +196,8 @@ pub fn plain_list_item<'r, 's>(
|
||||
)(remaining)?;
|
||||
|
||||
if !children.is_empty() && !context.should_consume_trailing_whitespace() {
|
||||
let final_item_context =
|
||||
parser_context.with_additional_node(ContextElement::ConsumeTrailingWhitespace(false));
|
||||
let final_item_context = ContextElement::ConsumeTrailingWhitespace(false);
|
||||
let final_item_context = parser_context.with_additional_node(&final_item_context);
|
||||
let (final_child_start, _original_final_child) = children
|
||||
.pop()
|
||||
.expect("if-statement already checked that children was non-empty.");
|
||||
@@ -205,25 +219,13 @@ pub fn plain_list_item<'r, 's>(
|
||||
indentation: indent_level,
|
||||
bullet: bull.into(),
|
||||
tag: maybe_tag
|
||||
.map(|(_ws, item_tag, _divider)| item_tag)
|
||||
.map(|(_ws, item_tag)| item_tag)
|
||||
.unwrap_or(Vec::new()),
|
||||
children: children.into_iter().map(|(_start, item)| item).collect(),
|
||||
},
|
||||
));
|
||||
}
|
||||
|
||||
fn include_input<'s, F, O>(
|
||||
mut inner: F,
|
||||
) -> impl FnMut(OrgSource<'s>) -> Res<OrgSource<'s>, (OrgSource<'s>, O)>
|
||||
where
|
||||
F: FnMut(OrgSource<'s>) -> Res<OrgSource<'s>, O>,
|
||||
{
|
||||
move |input: OrgSource<'_>| {
|
||||
let (remaining, output) = inner(input)?;
|
||||
Ok((remaining, (input, output)))
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg_attr(feature = "tracing", tracing::instrument(ret, level = "debug"))]
|
||||
fn bullet<'s>(i: OrgSource<'s>) -> Res<OrgSource<'s>, OrgSource<'s>> {
|
||||
alt((
|
||||
@@ -240,8 +242,8 @@ fn counter<'s>(i: OrgSource<'s>) -> Res<OrgSource<'s>, OrgSource<'s>> {
|
||||
}
|
||||
|
||||
#[cfg_attr(feature = "tracing", tracing::instrument(ret, level = "debug"))]
|
||||
fn plain_list_end<'r, 's>(
|
||||
_context: Context<'r, 's>,
|
||||
fn plain_list_end<'b, 'g, 'r, 's>(
|
||||
_context: RefContext<'b, 'g, 'r, 's>,
|
||||
input: OrgSource<'s>,
|
||||
) -> Res<OrgSource<'s>, OrgSource<'s>> {
|
||||
recognize(tuple((
|
||||
@@ -252,11 +254,9 @@ fn plain_list_end<'r, 's>(
|
||||
)))(input)
|
||||
}
|
||||
|
||||
const fn plain_list_item_end(
|
||||
indent_level: usize,
|
||||
) -> impl for<'r, 's> Fn(Context<'r, 's>, OrgSource<'s>) -> Res<OrgSource<'s>, OrgSource<'s>> {
|
||||
const fn plain_list_item_end(indent_level: usize) -> impl ContextMatcher {
|
||||
let line_indented_lte_matcher = line_indented_lte(indent_level);
|
||||
move |context: Context, input: OrgSource<'_>| {
|
||||
move |context, input: OrgSource<'_>| {
|
||||
_plain_list_item_end(context, input, &line_indented_lte_matcher)
|
||||
}
|
||||
}
|
||||
@@ -265,13 +265,10 @@ const fn plain_list_item_end(
|
||||
feature = "tracing",
|
||||
tracing::instrument(ret, level = "debug", skip(line_indented_lte_matcher))
|
||||
)]
|
||||
fn _plain_list_item_end<'r, 's>(
|
||||
context: Context<'r, 's>,
|
||||
fn _plain_list_item_end<'b, 'g, 'r, 's>(
|
||||
context: RefContext<'b, 'g, 'r, 's>,
|
||||
input: OrgSource<'s>,
|
||||
line_indented_lte_matcher: impl for<'rr, 'ss> Fn(
|
||||
Context<'rr, 'ss>,
|
||||
OrgSource<'ss>,
|
||||
) -> Res<OrgSource<'ss>, OrgSource<'ss>>,
|
||||
line_indented_lte_matcher: impl ContextMatcher,
|
||||
) -> Res<OrgSource<'s>, OrgSource<'s>> {
|
||||
start_of_line(input)?;
|
||||
recognize(tuple((
|
||||
@@ -280,15 +277,13 @@ fn _plain_list_item_end<'r, 's>(
|
||||
)))(input)
|
||||
}
|
||||
|
||||
const fn line_indented_lte(
|
||||
indent_level: usize,
|
||||
) -> impl for<'r, 's> Fn(Context<'r, 's>, OrgSource<'s>) -> Res<OrgSource<'s>, OrgSource<'s>> {
|
||||
move |context: Context, input: OrgSource<'_>| _line_indented_lte(context, input, indent_level)
|
||||
const fn line_indented_lte(indent_level: usize) -> impl ContextMatcher {
|
||||
move |context, input: OrgSource<'_>| _line_indented_lte(context, input, indent_level)
|
||||
}
|
||||
|
||||
#[cfg_attr(feature = "tracing", tracing::instrument(ret, level = "debug"))]
|
||||
fn _line_indented_lte<'r, 's>(
|
||||
_context: Context<'r, 's>,
|
||||
fn _line_indented_lte<'b, 'g, 'r, 's>(
|
||||
_context: RefContext<'b, 'g, 'r, 's>,
|
||||
input: OrgSource<'s>,
|
||||
indent_level: usize,
|
||||
) -> Res<OrgSource<'s>, OrgSource<'s>> {
|
||||
@@ -302,15 +297,22 @@ fn _line_indented_lte<'r, 's>(
|
||||
}
|
||||
|
||||
#[cfg_attr(feature = "tracing", tracing::instrument(ret, level = "debug"))]
|
||||
fn item_tag<'r, 's>(
|
||||
context: Context<'r, 's>,
|
||||
fn item_tag<'b, 'g, 'r, 's>(
|
||||
context: RefContext<'b, 'g, 'r, 's>,
|
||||
input: OrgSource<'s>,
|
||||
) -> Res<OrgSource<'s>, Vec<Object<'s>>> {
|
||||
let parser_context =
|
||||
context.with_additional_node(ContextElement::ExitMatcherNode(ExitMatcherNode {
|
||||
let contexts = [
|
||||
ContextElement::ExitMatcherNode(ExitMatcherNode {
|
||||
class: ExitClass::Gamma,
|
||||
exit_matcher: &item_tag_line_ending_end,
|
||||
}),
|
||||
ContextElement::ExitMatcherNode(ExitMatcherNode {
|
||||
class: ExitClass::Delta,
|
||||
exit_matcher: &item_tag_end,
|
||||
}));
|
||||
}),
|
||||
];
|
||||
let parser_context = context.with_additional_node(&contexts[0]);
|
||||
let parser_context = parser_context.with_additional_node(&contexts[1]);
|
||||
let (remaining, (children, _exit_contents)) = verify(
|
||||
many_till(
|
||||
// TODO: Should this be using a different set like the minimal set?
|
||||
@@ -319,24 +321,33 @@ fn item_tag<'r, 's>(
|
||||
),
|
||||
|(children, _exit_contents)| !children.is_empty(),
|
||||
)(input)?;
|
||||
let (remaining, _) = tuple((one_of(" \t"), tag("::")))(remaining)?;
|
||||
Ok((remaining, children))
|
||||
}
|
||||
|
||||
#[cfg_attr(feature = "tracing", tracing::instrument(ret, level = "debug"))]
|
||||
fn item_tag_end<'r, 's>(
|
||||
_context: Context<'r, 's>,
|
||||
fn item_tag_end<'b, 'g, 'r, 's>(
|
||||
_context: RefContext<'b, 'g, 'r, 's>,
|
||||
input: OrgSource<'s>,
|
||||
) -> Res<OrgSource<'s>, OrgSource<'s>> {
|
||||
recognize(alt((
|
||||
line_ending,
|
||||
tag(" :: "),
|
||||
recognize(tuple((tag(" ::"), alt((line_ending, eof))))),
|
||||
recognize(tuple((
|
||||
one_of(" \t"),
|
||||
tag("::"),
|
||||
alt((recognize(one_of(" \t")), line_ending, eof)),
|
||||
)))(input)
|
||||
}
|
||||
|
||||
#[cfg_attr(feature = "tracing", tracing::instrument(ret, level = "debug"))]
|
||||
fn item_tag_post_gap<'r, 's>(
|
||||
context: Context<'r, 's>,
|
||||
fn item_tag_line_ending_end<'b, 'g, 'r, 's>(
|
||||
_context: RefContext<'b, 'g, 'r, 's>,
|
||||
input: OrgSource<'s>,
|
||||
) -> Res<OrgSource<'s>, OrgSource<'s>> {
|
||||
line_ending(input)
|
||||
}
|
||||
|
||||
#[cfg_attr(feature = "tracing", tracing::instrument(ret, level = "debug"))]
|
||||
fn item_tag_post_gap<'b, 'g, 'r, 's>(
|
||||
context: RefContext<'b, 'g, 'r, 's>,
|
||||
input: OrgSource<'s>,
|
||||
) -> Res<OrgSource<'s>, OrgSource<'s>> {
|
||||
verify(
|
||||
@@ -355,17 +366,32 @@ fn item_tag_post_gap<'r, 's>(
|
||||
)(input)
|
||||
}
|
||||
|
||||
#[cfg_attr(feature = "tracing", tracing::instrument(ret, level = "debug"))]
|
||||
fn detect_contentless_item_contents<'b, 'g, 'r, 's>(
|
||||
context: RefContext<'b, 'g, 'r, 's>,
|
||||
input: OrgSource<'s>,
|
||||
) -> Res<OrgSource<'s>, ()> {
|
||||
let (remaining, _) = recognize(many_till(
|
||||
blank_line,
|
||||
parser_with_context!(exit_matcher_parser)(context),
|
||||
))(input)?;
|
||||
Ok((remaining, ()))
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
use crate::parser::parser_context::ContextTree;
|
||||
use crate::parser::parser_with_context::parser_with_context;
|
||||
use crate::parser::Source;
|
||||
use crate::context::Context;
|
||||
use crate::context::GlobalSettings;
|
||||
use crate::context::List;
|
||||
use crate::types::Source;
|
||||
|
||||
#[test]
|
||||
fn plain_list_item_empty() {
|
||||
let input = OrgSource::new("1.");
|
||||
let initial_context: ContextTree<'_, '_> = ContextTree::new();
|
||||
let global_settings = GlobalSettings::default();
|
||||
let initial_context = ContextElement::document_context();
|
||||
let initial_context = Context::new(&global_settings, List::new(&initial_context));
|
||||
let plain_list_item_matcher = parser_with_context!(plain_list_item)(&initial_context);
|
||||
let (remaining, result) = plain_list_item_matcher(input).unwrap();
|
||||
assert_eq!(Into::<&str>::into(remaining), "");
|
||||
@@ -375,7 +401,9 @@ mod tests {
|
||||
#[test]
|
||||
fn plain_list_item_simple() {
|
||||
let input = OrgSource::new("1. foo");
|
||||
let initial_context: ContextTree<'_, '_> = ContextTree::new();
|
||||
let global_settings = GlobalSettings::default();
|
||||
let initial_context = ContextElement::document_context();
|
||||
let initial_context = Context::new(&global_settings, List::new(&initial_context));
|
||||
let plain_list_item_matcher = parser_with_context!(plain_list_item)(&initial_context);
|
||||
let (remaining, result) = plain_list_item_matcher(input).unwrap();
|
||||
assert_eq!(Into::<&str>::into(remaining), "");
|
||||
@@ -385,7 +413,9 @@ mod tests {
|
||||
#[test]
|
||||
fn plain_list_empty() {
|
||||
let input = OrgSource::new("1.");
|
||||
let initial_context: ContextTree<'_, '_> = ContextTree::new();
|
||||
let global_settings = GlobalSettings::default();
|
||||
let initial_context = ContextElement::document_context();
|
||||
let initial_context = Context::new(&global_settings, List::new(&initial_context));
|
||||
let plain_list_matcher = parser_with_context!(plain_list)(&initial_context);
|
||||
let (remaining, result) = plain_list_matcher(input).unwrap();
|
||||
assert_eq!(Into::<&str>::into(remaining), "");
|
||||
@@ -395,7 +425,9 @@ mod tests {
|
||||
#[test]
|
||||
fn plain_list_simple() {
|
||||
let input = OrgSource::new("1. foo");
|
||||
let initial_context: ContextTree<'_, '_> = ContextTree::new();
|
||||
let global_settings = GlobalSettings::default();
|
||||
let initial_context = ContextElement::document_context();
|
||||
let initial_context = Context::new(&global_settings, List::new(&initial_context));
|
||||
let plain_list_matcher = parser_with_context!(plain_list)(&initial_context);
|
||||
let (remaining, result) = plain_list_matcher(input).unwrap();
|
||||
assert_eq!(Into::<&str>::into(remaining), "");
|
||||
@@ -406,7 +438,9 @@ mod tests {
|
||||
fn plain_list_cant_start_line_with_asterisk() {
|
||||
// Plain lists with an asterisk bullet must be indented or else they would be a headline
|
||||
let input = OrgSource::new("* foo");
|
||||
let initial_context: ContextTree<'_, '_> = ContextTree::new();
|
||||
let global_settings = GlobalSettings::default();
|
||||
let initial_context = ContextElement::document_context();
|
||||
let initial_context = Context::new(&global_settings, List::new(&initial_context));
|
||||
let plain_list_matcher = parser_with_context!(plain_list)(&initial_context);
|
||||
let result = plain_list_matcher(input);
|
||||
assert!(result.is_err());
|
||||
@@ -416,7 +450,9 @@ mod tests {
|
||||
fn indented_can_start_line_with_asterisk() {
|
||||
// Plain lists with an asterisk bullet must be indented or else they would be a headline
|
||||
let input = OrgSource::new(" * foo");
|
||||
let initial_context: ContextTree<'_, '_> = ContextTree::new();
|
||||
let global_settings = GlobalSettings::default();
|
||||
let initial_context = ContextElement::document_context();
|
||||
let initial_context = Context::new(&global_settings, List::new(&initial_context));
|
||||
let plain_list_matcher = parser_with_context!(plain_list)(&initial_context);
|
||||
let result = plain_list_matcher(input);
|
||||
assert!(result.is_ok());
|
||||
@@ -434,7 +470,9 @@ mod tests {
|
||||
ipsum
|
||||
"#,
|
||||
);
|
||||
let initial_context: ContextTree<'_, '_> = ContextTree::new();
|
||||
let global_settings = GlobalSettings::default();
|
||||
let initial_context = ContextElement::document_context();
|
||||
let initial_context = Context::new(&global_settings, List::new(&initial_context));
|
||||
let plain_list_matcher = parser_with_context!(element(true))(&initial_context);
|
||||
let (remaining, result) =
|
||||
plain_list_matcher(input).expect("Should parse the plain list successfully.");
|
||||
@@ -460,7 +498,9 @@ mod tests {
|
||||
|
||||
baz"#,
|
||||
);
|
||||
let initial_context: ContextTree<'_, '_> = ContextTree::new();
|
||||
let global_settings = GlobalSettings::default();
|
||||
let initial_context = ContextElement::document_context();
|
||||
let initial_context = Context::new(&global_settings, List::new(&initial_context));
|
||||
let plain_list_matcher = parser_with_context!(element(true))(&initial_context);
|
||||
let (remaining, result) =
|
||||
plain_list_matcher(input).expect("Should parse the plain list successfully.");
|
||||
@@ -491,7 +531,9 @@ baz"#,
|
||||
|
||||
dolar"#,
|
||||
);
|
||||
let initial_context: ContextTree<'_, '_> = ContextTree::new();
|
||||
let global_settings = GlobalSettings::default();
|
||||
let initial_context = ContextElement::document_context();
|
||||
let initial_context = Context::new(&global_settings, List::new(&initial_context));
|
||||
let plain_list_matcher = parser_with_context!(element(true))(&initial_context);
|
||||
let (remaining, result) =
|
||||
plain_list_matcher(input).expect("Should parse the plain list successfully.");
|
||||
|
||||
@@ -7,27 +7,51 @@ use nom::combinator::recognize;
|
||||
use nom::combinator::verify;
|
||||
use nom::multi::many_till;
|
||||
|
||||
use super::object::PlainText;
|
||||
use super::org_source::OrgSource;
|
||||
use super::radio_link::RematchObject;
|
||||
use super::Context;
|
||||
use super::Object;
|
||||
use super::util::exit_matcher_parser;
|
||||
use crate::context::parser_with_context;
|
||||
use crate::context::RefContext;
|
||||
use crate::error::Res;
|
||||
use crate::parser::object_parser::any_object_except_plain_text;
|
||||
use crate::parser::parser_with_context::parser_with_context;
|
||||
use crate::parser::util::exit_matcher_parser;
|
||||
use crate::types::Object;
|
||||
use crate::types::PlainText;
|
||||
|
||||
#[cfg_attr(feature = "tracing", tracing::instrument(ret, level = "debug"))]
|
||||
pub fn plain_text<'r, 's>(
|
||||
context: Context<'r, 's>,
|
||||
pub fn plain_text<F>(
|
||||
end_condition: F,
|
||||
) -> impl for<'b, 'g, 'r, 's> Fn(
|
||||
RefContext<'b, 'g, 'r, 's>,
|
||||
OrgSource<'s>,
|
||||
) -> Res<OrgSource<'s>, PlainText<'s>>
|
||||
where
|
||||
F: for<'bb, 'gg, 'rr, 'ss> Fn(
|
||||
RefContext<'bb, 'gg, 'rr, 'ss>,
|
||||
OrgSource<'ss>,
|
||||
) -> Res<OrgSource<'ss>, ()>,
|
||||
{
|
||||
move |context, input| _plain_text(&end_condition, context, input)
|
||||
}
|
||||
|
||||
#[cfg_attr(
|
||||
feature = "tracing",
|
||||
tracing::instrument(ret, level = "debug", skip(end_condition))
|
||||
)]
|
||||
fn _plain_text<'b, 'g, 'r, 's, F>(
|
||||
end_condition: F,
|
||||
context: RefContext<'b, 'g, 'r, 's>,
|
||||
input: OrgSource<'s>,
|
||||
) -> Res<OrgSource<'s>, PlainText<'s>> {
|
||||
) -> Res<OrgSource<'s>, PlainText<'s>>
|
||||
where
|
||||
F: for<'bb, 'gg, 'rr, 'ss> Fn(
|
||||
RefContext<'bb, 'gg, 'rr, 'ss>,
|
||||
OrgSource<'ss>,
|
||||
) -> Res<OrgSource<'ss>, ()>,
|
||||
{
|
||||
let (remaining, source) = recognize(verify(
|
||||
many_till(
|
||||
anychar,
|
||||
peek(alt((
|
||||
parser_with_context!(exit_matcher_parser)(context),
|
||||
parser_with_context!(plain_text_end)(context),
|
||||
recognize(parser_with_context!(end_condition)(context)),
|
||||
))),
|
||||
),
|
||||
|(children, _exit_contents)| !children.is_empty(),
|
||||
@@ -41,19 +65,11 @@ pub fn plain_text<'r, 's>(
|
||||
))
|
||||
}
|
||||
|
||||
#[cfg_attr(feature = "tracing", tracing::instrument(ret, level = "debug"))]
|
||||
fn plain_text_end<'r, 's>(
|
||||
context: Context<'r, 's>,
|
||||
input: OrgSource<'s>,
|
||||
) -> Res<OrgSource<'s>, OrgSource<'s>> {
|
||||
recognize(parser_with_context!(any_object_except_plain_text)(context))(input)
|
||||
}
|
||||
|
||||
impl<'x> RematchObject<'x> for PlainText<'x> {
|
||||
#[cfg_attr(feature = "tracing", tracing::instrument(ret, level = "debug"))]
|
||||
fn rematch_object<'r, 's>(
|
||||
fn rematch_object<'b, 'g, 'r, 's>(
|
||||
&'x self,
|
||||
_context: Context<'r, 's>,
|
||||
_context: RefContext<'b, 'g, 'r, 's>,
|
||||
input: OrgSource<'s>,
|
||||
) -> Res<OrgSource<'s>, Object<'s>> {
|
||||
map(tag(self.source), |s| {
|
||||
@@ -69,15 +85,22 @@ mod tests {
|
||||
use nom::combinator::map;
|
||||
|
||||
use super::*;
|
||||
use crate::parser::parser_context::ContextTree;
|
||||
use crate::parser::parser_with_context::parser_with_context;
|
||||
use crate::parser::source::Source;
|
||||
use crate::context::Context;
|
||||
use crate::context::ContextElement;
|
||||
use crate::context::GlobalSettings;
|
||||
use crate::context::List;
|
||||
use crate::parser::object_parser::detect_standard_set_object_sans_plain_text;
|
||||
use crate::types::Source;
|
||||
|
||||
#[test]
|
||||
fn plain_text_simple() {
|
||||
let input = OrgSource::new("foobarbaz");
|
||||
let initial_context: ContextTree<'_, '_> = ContextTree::new();
|
||||
let plain_text_matcher = parser_with_context!(plain_text)(&initial_context);
|
||||
let global_settings = GlobalSettings::default();
|
||||
let initial_context = ContextElement::document_context();
|
||||
let initial_context = Context::new(&global_settings, List::new(&initial_context));
|
||||
let plain_text_matcher = parser_with_context!(plain_text(
|
||||
detect_standard_set_object_sans_plain_text
|
||||
))(&initial_context);
|
||||
let (remaining, result) = map(plain_text_matcher, Object::PlainText)(input).unwrap();
|
||||
assert_eq!(Into::<&str>::into(remaining), "");
|
||||
assert_eq!(result.get_source(), Into::<&str>::into(input));
|
||||
|
||||
@@ -10,15 +10,16 @@ use nom::multi::separated_list1;
|
||||
use nom::sequence::tuple;
|
||||
|
||||
use super::org_source::OrgSource;
|
||||
use super::Context;
|
||||
use super::util::maybe_consume_trailing_whitespace_if_not_exiting;
|
||||
use crate::context::RefContext;
|
||||
use crate::error::Res;
|
||||
use crate::parser::lesser_element::Planning;
|
||||
use crate::parser::util::get_consumed;
|
||||
use crate::parser::util::start_of_line;
|
||||
use crate::types::Planning;
|
||||
|
||||
#[cfg_attr(feature = "tracing", tracing::instrument(ret, level = "debug"))]
|
||||
pub fn planning<'r, 's>(
|
||||
_context: Context<'r, 's>,
|
||||
pub fn planning<'b, 'g, 'r, 's>(
|
||||
context: RefContext<'b, 'g, 'r, 's>,
|
||||
input: OrgSource<'s>,
|
||||
) -> Res<OrgSource<'s>, Planning<'s>> {
|
||||
start_of_line(input)?;
|
||||
@@ -26,6 +27,8 @@ pub fn planning<'r, 's>(
|
||||
let (remaining, _planning_parameters) = separated_list1(space1, planning_parameter)(remaining)?;
|
||||
let (remaining, _trailing_ws) = tuple((space0, alt((line_ending, eof))))(remaining)?;
|
||||
|
||||
let (remaining, _trailing_ws) =
|
||||
maybe_consume_trailing_whitespace_if_not_exiting(context, remaining)?;
|
||||
let source = get_consumed(input, remaining);
|
||||
|
||||
Ok((
|
||||
|
||||
@@ -14,25 +14,25 @@ use nom::multi::many_till;
|
||||
use nom::sequence::tuple;
|
||||
|
||||
use super::org_source::OrgSource;
|
||||
use super::Context;
|
||||
use super::util::exit_matcher_parser;
|
||||
use crate::context::parser_with_context;
|
||||
use crate::context::ContextElement;
|
||||
use crate::context::ExitClass;
|
||||
use crate::context::ExitMatcherNode;
|
||||
use crate::context::RefContext;
|
||||
use crate::error::CustomError;
|
||||
use crate::error::MyError;
|
||||
use crate::error::Res;
|
||||
use crate::parser::exiting::ExitClass;
|
||||
use crate::parser::greater_element::NodeProperty;
|
||||
use crate::parser::greater_element::PropertyDrawer;
|
||||
use crate::parser::parser_context::ContextElement;
|
||||
use crate::parser::parser_context::ExitMatcherNode;
|
||||
use crate::parser::parser_with_context::parser_with_context;
|
||||
use crate::parser::util::exit_matcher_parser;
|
||||
use crate::parser::util::get_consumed;
|
||||
use crate::parser::util::immediate_in_section;
|
||||
use crate::parser::util::maybe_consume_trailing_whitespace_if_not_exiting;
|
||||
use crate::parser::util::start_of_line;
|
||||
use crate::types::NodeProperty;
|
||||
use crate::types::PropertyDrawer;
|
||||
|
||||
#[cfg_attr(feature = "tracing", tracing::instrument(ret, level = "debug"))]
|
||||
pub fn property_drawer<'r, 's>(
|
||||
context: Context<'r, 's>,
|
||||
pub fn property_drawer<'b, 'g, 'r, 's>(
|
||||
context: RefContext<'b, 'g, 'r, 's>,
|
||||
input: OrgSource<'s>,
|
||||
) -> Res<OrgSource<'s>, PropertyDrawer<'s>> {
|
||||
if immediate_in_section(context, "property-drawer") {
|
||||
@@ -51,13 +51,17 @@ pub fn property_drawer<'r, 's>(
|
||||
line_ending,
|
||||
))(input)?;
|
||||
|
||||
let parser_context = context
|
||||
.with_additional_node(ContextElement::ConsumeTrailingWhitespace(true))
|
||||
.with_additional_node(ContextElement::Context("property-drawer"))
|
||||
.with_additional_node(ContextElement::ExitMatcherNode(ExitMatcherNode {
|
||||
let contexts = [
|
||||
ContextElement::ConsumeTrailingWhitespace(true),
|
||||
ContextElement::Context("property-drawer"),
|
||||
ContextElement::ExitMatcherNode(ExitMatcherNode {
|
||||
class: ExitClass::Alpha,
|
||||
exit_matcher: &property_drawer_end,
|
||||
}));
|
||||
}),
|
||||
];
|
||||
let parser_context = context.with_additional_node(&contexts[0]);
|
||||
let parser_context = parser_context.with_additional_node(&contexts[1]);
|
||||
let parser_context = parser_context.with_additional_node(&contexts[2]);
|
||||
|
||||
let node_property_matcher = parser_with_context!(node_property)(&parser_context);
|
||||
let exit_matcher = parser_with_context!(exit_matcher_parser)(&parser_context);
|
||||
@@ -79,8 +83,8 @@ pub fn property_drawer<'r, 's>(
|
||||
}
|
||||
|
||||
#[cfg_attr(feature = "tracing", tracing::instrument(ret, level = "debug"))]
|
||||
fn property_drawer_end<'r, 's>(
|
||||
_context: Context<'r, 's>,
|
||||
fn property_drawer_end<'b, 'g, 'r, 's>(
|
||||
_context: RefContext<'b, 'g, 'r, 's>,
|
||||
input: OrgSource<'s>,
|
||||
) -> Res<OrgSource<'s>, OrgSource<'s>> {
|
||||
recognize(tuple((
|
||||
@@ -93,8 +97,8 @@ fn property_drawer_end<'r, 's>(
|
||||
}
|
||||
|
||||
#[cfg_attr(feature = "tracing", tracing::instrument(ret, level = "debug"))]
|
||||
fn node_property<'r, 's>(
|
||||
context: Context<'r, 's>,
|
||||
fn node_property<'b, 'g, 'r, 's>(
|
||||
context: RefContext<'b, 'g, 'r, 's>,
|
||||
input: OrgSource<'s>,
|
||||
) -> Res<OrgSource<'s>, NodeProperty<'s>> {
|
||||
let (remaining, (_start_of_line, _leading_whitespace, _open_colon, _name, _close_colon)) =
|
||||
@@ -136,15 +140,15 @@ fn node_property<'r, 's>(
|
||||
}
|
||||
|
||||
#[cfg_attr(feature = "tracing", tracing::instrument(ret, level = "debug"))]
|
||||
fn node_property_name<'r, 's>(
|
||||
context: Context<'r, 's>,
|
||||
fn node_property_name<'b, 'g, 'r, 's>(
|
||||
context: RefContext<'b, 'g, 'r, 's>,
|
||||
input: OrgSource<'s>,
|
||||
) -> Res<OrgSource<'s>, OrgSource<'s>> {
|
||||
let parser_context =
|
||||
context.with_additional_node(ContextElement::ExitMatcherNode(ExitMatcherNode {
|
||||
class: ExitClass::Beta,
|
||||
exit_matcher: &node_property_name_end,
|
||||
}));
|
||||
let parser_context = ContextElement::ExitMatcherNode(ExitMatcherNode {
|
||||
class: ExitClass::Beta,
|
||||
exit_matcher: &node_property_name_end,
|
||||
});
|
||||
let parser_context = context.with_additional_node(&parser_context);
|
||||
|
||||
let (remaining, name) = recognize(tuple((
|
||||
verify(
|
||||
@@ -161,9 +165,12 @@ fn node_property_name<'r, 's>(
|
||||
}
|
||||
|
||||
#[cfg_attr(feature = "tracing", tracing::instrument(ret, level = "debug"))]
|
||||
fn node_property_name_end<'r, 's>(
|
||||
_context: Context<'r, 's>,
|
||||
fn node_property_name_end<'b, 'g, 'r, 's>(
|
||||
_context: RefContext<'b, 'g, 'r, 's>,
|
||||
input: OrgSource<'s>,
|
||||
) -> Res<OrgSource<'s>, OrgSource<'s>> {
|
||||
alt((tag("+:"), tag(":")))(input)
|
||||
recognize(tuple((
|
||||
alt((tag("+:"), tag(":"))),
|
||||
alt((space1, line_ending, eof)),
|
||||
)))(input)
|
||||
}
|
||||
|
||||
@@ -5,36 +5,29 @@ use nom::character::complete::space0;
|
||||
use nom::combinator::verify;
|
||||
use nom::multi::many_till;
|
||||
|
||||
use super::object_parser::minimal_set_object;
|
||||
use super::org_source::OrgSource;
|
||||
use super::util::exit_matcher_parser;
|
||||
use super::util::maybe_consume_object_trailing_whitespace_if_not_exiting;
|
||||
use super::Context;
|
||||
use super::Object;
|
||||
use crate::context::parser_with_context;
|
||||
use crate::context::ContextElement;
|
||||
use crate::context::ExitClass;
|
||||
use crate::context::ExitMatcherNode;
|
||||
use crate::context::RefContext;
|
||||
use crate::error::CustomError;
|
||||
use crate::error::MyError;
|
||||
use crate::error::Res;
|
||||
use crate::parser::exiting::ExitClass;
|
||||
use crate::parser::object_parser::minimal_set_object;
|
||||
use crate::parser::parser_context::ContextElement;
|
||||
use crate::parser::parser_context::ExitMatcherNode;
|
||||
use crate::parser::parser_with_context::parser_with_context;
|
||||
use crate::parser::util::exit_matcher_parser;
|
||||
use crate::parser::util::get_consumed;
|
||||
use crate::parser::RadioLink;
|
||||
use crate::parser::RadioTarget;
|
||||
use crate::types::Object;
|
||||
use crate::types::RadioLink;
|
||||
use crate::types::RadioTarget;
|
||||
|
||||
#[cfg_attr(feature = "tracing", tracing::instrument(ret, level = "debug"))]
|
||||
pub fn radio_link<'r, 's>(
|
||||
context: Context<'r, 's>,
|
||||
pub fn radio_link<'b, 'g, 'r, 's>(
|
||||
context: RefContext<'b, 'g, 'r, 's>,
|
||||
input: OrgSource<'s>,
|
||||
) -> Res<OrgSource<'s>, RadioLink<'s>> {
|
||||
let radio_targets = context
|
||||
.iter()
|
||||
.filter_map(|context_element| match context_element.get_data() {
|
||||
ContextElement::RadioTarget(targets) => Some(targets),
|
||||
_ => None,
|
||||
})
|
||||
.flatten();
|
||||
for radio_target in radio_targets {
|
||||
for radio_target in &context.get_global_settings().radio_targets {
|
||||
let rematched_target = rematch_target(context, radio_target, input);
|
||||
if let Ok((remaining, rematched_target)) = rematched_target {
|
||||
let (remaining, _) = space0(remaining)?;
|
||||
@@ -54,8 +47,8 @@ pub fn radio_link<'r, 's>(
|
||||
}
|
||||
|
||||
#[cfg_attr(feature = "tracing", tracing::instrument(ret, level = "debug"))]
|
||||
pub fn rematch_target<'x, 'r, 's>(
|
||||
context: Context<'r, 's>,
|
||||
pub fn rematch_target<'x, 'b, 'g, 'r, 's>(
|
||||
context: RefContext<'b, 'g, 'r, 's>,
|
||||
target: &'x Vec<Object<'x>>,
|
||||
input: OrgSource<'s>,
|
||||
) -> Res<OrgSource<'s>, Vec<Object<'s>>> {
|
||||
@@ -85,16 +78,16 @@ pub fn rematch_target<'x, 'r, 's>(
|
||||
}
|
||||
|
||||
#[cfg_attr(feature = "tracing", tracing::instrument(ret, level = "debug"))]
|
||||
pub fn radio_target<'r, 's>(
|
||||
context: Context<'r, 's>,
|
||||
pub fn radio_target<'b, 'g, 'r, 's>(
|
||||
context: RefContext<'b, 'g, 'r, 's>,
|
||||
input: OrgSource<'s>,
|
||||
) -> Res<OrgSource<'s>, RadioTarget<'s>> {
|
||||
let (remaining, _opening) = tag("<<<")(input)?;
|
||||
let parser_context =
|
||||
context.with_additional_node(ContextElement::ExitMatcherNode(ExitMatcherNode {
|
||||
class: ExitClass::Gamma,
|
||||
exit_matcher: &radio_target_end,
|
||||
}));
|
||||
let parser_context = ContextElement::ExitMatcherNode(ExitMatcherNode {
|
||||
class: ExitClass::Gamma,
|
||||
exit_matcher: &radio_target_end,
|
||||
});
|
||||
let parser_context = context.with_additional_node(&parser_context);
|
||||
|
||||
let (remaining, (children, _exit_contents)) = verify(
|
||||
many_till(
|
||||
@@ -118,17 +111,17 @@ pub fn radio_target<'r, 's>(
|
||||
}
|
||||
|
||||
#[cfg_attr(feature = "tracing", tracing::instrument(ret, level = "debug"))]
|
||||
fn radio_target_end<'r, 's>(
|
||||
_context: Context<'r, 's>,
|
||||
fn radio_target_end<'b, 'g, 'r, 's>(
|
||||
_context: RefContext<'b, 'g, 'r, 's>,
|
||||
input: OrgSource<'s>,
|
||||
) -> Res<OrgSource<'s>, OrgSource<'s>> {
|
||||
alt((tag("<"), tag(">"), line_ending))(input)
|
||||
}
|
||||
|
||||
pub trait RematchObject<'x> {
|
||||
fn rematch_object<'r, 's>(
|
||||
fn rematch_object<'b, 'g, 'r, 's>(
|
||||
&'x self,
|
||||
context: Context<'r, 's>,
|
||||
context: RefContext<'b, 'g, 'r, 's>,
|
||||
input: OrgSource<'s>,
|
||||
) -> Res<OrgSource<'s>, Object<'s>>;
|
||||
}
|
||||
@@ -136,25 +129,30 @@ pub trait RematchObject<'x> {
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
use crate::context::Context;
|
||||
use crate::context::GlobalSettings;
|
||||
use crate::context::List;
|
||||
use crate::parser::element_parser::element;
|
||||
use crate::parser::parser_context::ContextElement;
|
||||
use crate::parser::parser_context::ContextTree;
|
||||
use crate::parser::parser_with_context::parser_with_context;
|
||||
use crate::parser::source::Source;
|
||||
use crate::parser::Bold;
|
||||
use crate::parser::PlainText;
|
||||
use crate::types::Bold;
|
||||
use crate::types::Element;
|
||||
use crate::types::PlainText;
|
||||
use crate::types::Source;
|
||||
|
||||
#[test]
|
||||
fn plain_text_radio_target() {
|
||||
let input = OrgSource::new("foo bar baz");
|
||||
let radio_target_match = vec![Object::PlainText(PlainText { source: "bar" })];
|
||||
let initial_context: ContextTree<'_, '_> = ContextTree::new();
|
||||
let document_context = initial_context
|
||||
.with_additional_node(ContextElement::RadioTarget(vec![&radio_target_match]));
|
||||
let paragraph_matcher = parser_with_context!(element(true))(&document_context);
|
||||
let global_settings = {
|
||||
let mut global_settings = GlobalSettings::default();
|
||||
global_settings.radio_targets = vec![&radio_target_match];
|
||||
global_settings
|
||||
};
|
||||
let initial_context = ContextElement::document_context();
|
||||
let initial_context = Context::new(&global_settings, List::new(&initial_context));
|
||||
let paragraph_matcher = parser_with_context!(element(true))(&initial_context);
|
||||
let (remaining, first_paragraph) = paragraph_matcher(input).expect("Parse first paragraph");
|
||||
let first_paragraph = match first_paragraph {
|
||||
crate::parser::Element::Paragraph(paragraph) => paragraph,
|
||||
Element::Paragraph(paragraph) => paragraph,
|
||||
_ => panic!("Should be a paragraph!"),
|
||||
};
|
||||
assert_eq!(Into::<&str>::into(remaining), "");
|
||||
@@ -179,14 +177,18 @@ mod tests {
|
||||
source: "*bar*",
|
||||
children: vec![Object::PlainText(PlainText { source: "bar" })],
|
||||
})];
|
||||
let initial_context: ContextTree<'_, '_> = ContextTree::new();
|
||||
let document_context = initial_context
|
||||
.with_additional_node(ContextElement::RadioTarget(vec![&radio_target_match]));
|
||||
let paragraph_matcher = parser_with_context!(element(true))(&document_context);
|
||||
let global_settings = {
|
||||
let mut global_settings = GlobalSettings::default();
|
||||
global_settings.radio_targets = vec![&radio_target_match];
|
||||
global_settings
|
||||
};
|
||||
let initial_context = ContextElement::document_context();
|
||||
let initial_context = Context::new(&global_settings, List::new(&initial_context));
|
||||
let paragraph_matcher = parser_with_context!(element(true))(&initial_context);
|
||||
let (remaining, first_paragraph) =
|
||||
paragraph_matcher(input.into()).expect("Parse first paragraph");
|
||||
let first_paragraph = match first_paragraph {
|
||||
crate::parser::Element::Paragraph(paragraph) => paragraph,
|
||||
Element::Paragraph(paragraph) => paragraph,
|
||||
_ => panic!("Should be a paragraph!"),
|
||||
};
|
||||
assert_eq!(Into::<&str>::into(remaining), "");
|
||||
|
||||
@@ -6,23 +6,23 @@ use nom::character::complete::one_of;
|
||||
use nom::combinator::verify;
|
||||
use nom::multi::many_till;
|
||||
|
||||
use super::object_parser::regular_link_description_set_object;
|
||||
use super::org_source::OrgSource;
|
||||
use super::parser_with_context::parser_with_context;
|
||||
use super::util::exit_matcher_parser;
|
||||
use super::util::get_consumed;
|
||||
use super::util::maybe_consume_object_trailing_whitespace_if_not_exiting;
|
||||
use super::Context;
|
||||
use super::Object;
|
||||
use super::RegularLink;
|
||||
use crate::context::parser_with_context;
|
||||
use crate::context::ContextElement;
|
||||
use crate::context::ExitClass;
|
||||
use crate::context::ExitMatcherNode;
|
||||
use crate::context::RefContext;
|
||||
use crate::error::Res;
|
||||
use crate::parser::exiting::ExitClass;
|
||||
use crate::parser::object_parser::regular_link_description_object_set;
|
||||
use crate::parser::parser_context::ContextElement;
|
||||
use crate::parser::parser_context::ExitMatcherNode;
|
||||
use crate::parser::util::exit_matcher_parser;
|
||||
use crate::types::Object;
|
||||
use crate::types::RegularLink;
|
||||
|
||||
#[cfg_attr(feature = "tracing", tracing::instrument(ret, level = "debug"))]
|
||||
pub fn regular_link<'r, 's>(
|
||||
context: Context<'r, 's>,
|
||||
pub fn regular_link<'b, 'g, 'r, 's>(
|
||||
context: RefContext<'b, 'g, 'r, 's>,
|
||||
input: OrgSource<'s>,
|
||||
) -> Res<OrgSource<'s>, RegularLink<'s>> {
|
||||
alt((
|
||||
@@ -32,8 +32,8 @@ pub fn regular_link<'r, 's>(
|
||||
}
|
||||
|
||||
#[cfg_attr(feature = "tracing", tracing::instrument(ret, level = "debug"))]
|
||||
pub fn regular_link_without_description<'r, 's>(
|
||||
context: Context<'r, 's>,
|
||||
pub fn regular_link_without_description<'b, 'g, 'r, 's>(
|
||||
context: RefContext<'b, 'g, 'r, 's>,
|
||||
input: OrgSource<'s>,
|
||||
) -> Res<OrgSource<'s>, RegularLink<'s>> {
|
||||
let (remaining, _opening_bracket) = tag("[[")(input)?;
|
||||
@@ -51,8 +51,8 @@ pub fn regular_link_without_description<'r, 's>(
|
||||
}
|
||||
|
||||
#[cfg_attr(feature = "tracing", tracing::instrument(ret, level = "debug"))]
|
||||
pub fn regular_link_with_description<'r, 's>(
|
||||
context: Context<'r, 's>,
|
||||
pub fn regular_link_with_description<'b, 'g, 'r, 's>(
|
||||
context: RefContext<'b, 'g, 'r, 's>,
|
||||
input: OrgSource<'s>,
|
||||
) -> Res<OrgSource<'s>, RegularLink<'s>> {
|
||||
let (remaining, _opening_bracket) = tag("[[")(input)?;
|
||||
@@ -72,8 +72,8 @@ pub fn regular_link_with_description<'r, 's>(
|
||||
}
|
||||
|
||||
#[cfg_attr(feature = "tracing", tracing::instrument(ret, level = "debug"))]
|
||||
pub fn pathreg<'r, 's>(
|
||||
_context: Context<'r, 's>,
|
||||
pub fn pathreg<'b, 'g, 'r, 's>(
|
||||
_context: RefContext<'b, 'g, 'r, 's>,
|
||||
input: OrgSource<'s>,
|
||||
) -> Res<OrgSource<'s>, OrgSource<'s>> {
|
||||
let (remaining, path) = escaped(
|
||||
@@ -88,18 +88,18 @@ pub fn pathreg<'r, 's>(
|
||||
}
|
||||
|
||||
#[cfg_attr(feature = "tracing", tracing::instrument(ret, level = "debug"))]
|
||||
pub fn description<'r, 's>(
|
||||
context: Context<'r, 's>,
|
||||
pub fn description<'b, 'g, 'r, 's>(
|
||||
context: RefContext<'b, 'g, 'r, 's>,
|
||||
input: OrgSource<'s>,
|
||||
) -> Res<OrgSource<'s>, Vec<Object<'s>>> {
|
||||
let parser_context =
|
||||
context.with_additional_node(ContextElement::ExitMatcherNode(ExitMatcherNode {
|
||||
class: ExitClass::Beta,
|
||||
exit_matcher: &description_end,
|
||||
}));
|
||||
let parser_context = ContextElement::ExitMatcherNode(ExitMatcherNode {
|
||||
class: ExitClass::Beta,
|
||||
exit_matcher: &description_end,
|
||||
});
|
||||
let parser_context = context.with_additional_node(&parser_context);
|
||||
let (remaining, (children, _exit_contents)) = verify(
|
||||
many_till(
|
||||
parser_with_context!(regular_link_description_object_set)(&parser_context),
|
||||
parser_with_context!(regular_link_description_set_object)(&parser_context),
|
||||
parser_with_context!(exit_matcher_parser)(&parser_context),
|
||||
),
|
||||
|(children, _exit_contents)| !children.is_empty(),
|
||||
@@ -109,8 +109,8 @@ pub fn description<'r, 's>(
|
||||
}
|
||||
|
||||
#[cfg_attr(feature = "tracing", tracing::instrument(ret, level = "debug"))]
|
||||
fn description_end<'r, 's>(
|
||||
_context: Context<'r, 's>,
|
||||
fn description_end<'b, 'g, 'r, 's>(
|
||||
_context: RefContext<'b, 'g, 'r, 's>,
|
||||
input: OrgSource<'s>,
|
||||
) -> Res<OrgSource<'s>, OrgSource<'s>> {
|
||||
tag("]]")(input)
|
||||
|
||||
144
src/parser/section.rs
Normal file
144
src/parser/section.rs
Normal file
@@ -0,0 +1,144 @@
|
||||
use nom::combinator::opt;
|
||||
use nom::combinator::recognize;
|
||||
use nom::combinator::verify;
|
||||
use nom::multi::many0;
|
||||
use nom::multi::many_till;
|
||||
use nom::sequence::tuple;
|
||||
|
||||
use super::headline::detect_headline;
|
||||
use super::org_source::OrgSource;
|
||||
use super::util::exit_matcher_parser;
|
||||
use super::util::get_consumed;
|
||||
use crate::context::parser_with_context;
|
||||
use crate::context::ContextElement;
|
||||
use crate::context::ExitClass;
|
||||
use crate::context::ExitMatcherNode;
|
||||
use crate::context::RefContext;
|
||||
use crate::error::Res;
|
||||
use crate::parser::comment::comment;
|
||||
use crate::parser::element_parser::element;
|
||||
use crate::parser::planning::planning;
|
||||
use crate::parser::property_drawer::property_drawer;
|
||||
use crate::parser::util::blank_line;
|
||||
use crate::parser::util::maybe_consume_trailing_whitespace_if_not_exiting;
|
||||
use crate::types::Element;
|
||||
use crate::types::Section;
|
||||
|
||||
#[cfg_attr(feature = "tracing", tracing::instrument(ret, level = "debug"))]
|
||||
pub fn zeroth_section<'b, 'g, 'r, 's>(
|
||||
context: RefContext<'b, 'g, 'r, 's>,
|
||||
input: OrgSource<'s>,
|
||||
) -> Res<OrgSource<'s>, Section<'s>> {
|
||||
let contexts = [
|
||||
ContextElement::ConsumeTrailingWhitespace(true),
|
||||
ContextElement::Context("section"),
|
||||
ContextElement::ExitMatcherNode(ExitMatcherNode {
|
||||
class: ExitClass::Document,
|
||||
exit_matcher: §ion_end,
|
||||
}),
|
||||
];
|
||||
let parser_context = context.with_additional_node(&contexts[0]);
|
||||
let parser_context = parser_context.with_additional_node(&contexts[1]);
|
||||
let parser_context = parser_context.with_additional_node(&contexts[2]);
|
||||
let without_consuming_whitespace_context = ContextElement::ConsumeTrailingWhitespace(false);
|
||||
let without_consuming_whitespace_context =
|
||||
parser_context.with_additional_node(&without_consuming_whitespace_context);
|
||||
|
||||
let element_matcher = parser_with_context!(element(true))(&parser_context);
|
||||
let exit_matcher = parser_with_context!(exit_matcher_parser)(&parser_context);
|
||||
|
||||
let (remaining, comment_and_property_drawer_element) = opt(tuple((
|
||||
opt(parser_with_context!(comment)(
|
||||
&without_consuming_whitespace_context,
|
||||
)),
|
||||
parser_with_context!(property_drawer)(context),
|
||||
many0(blank_line),
|
||||
)))(input)?;
|
||||
|
||||
let (remaining, (mut children, _exit_contents)) = verify(
|
||||
many_till(element_matcher, exit_matcher),
|
||||
|(children, _exit_contents)| {
|
||||
!children.is_empty() || comment_and_property_drawer_element.is_some()
|
||||
},
|
||||
)(remaining)?;
|
||||
|
||||
comment_and_property_drawer_element.map(|(comment, property_drawer, _ws)| {
|
||||
children.insert(0, Element::PropertyDrawer(property_drawer));
|
||||
comment
|
||||
.map(Element::Comment)
|
||||
.map(|ele| children.insert(0, ele));
|
||||
});
|
||||
|
||||
let (remaining, _trailing_ws) =
|
||||
maybe_consume_trailing_whitespace_if_not_exiting(context, remaining)?;
|
||||
|
||||
let source = get_consumed(input, remaining);
|
||||
Ok((
|
||||
remaining,
|
||||
Section {
|
||||
source: source.into(),
|
||||
children,
|
||||
},
|
||||
))
|
||||
}
|
||||
|
||||
#[cfg_attr(feature = "tracing", tracing::instrument(ret, level = "debug"))]
|
||||
pub fn section<'b, 'g, 'r, 's>(
|
||||
context: RefContext<'b, 'g, 'r, 's>,
|
||||
mut input: OrgSource<'s>,
|
||||
) -> Res<OrgSource<'s>, Section<'s>> {
|
||||
let contexts = [
|
||||
ContextElement::ConsumeTrailingWhitespace(true),
|
||||
ContextElement::Context("section"),
|
||||
ContextElement::ExitMatcherNode(ExitMatcherNode {
|
||||
class: ExitClass::Document,
|
||||
exit_matcher: §ion_end,
|
||||
}),
|
||||
];
|
||||
let parser_context = context.with_additional_node(&contexts[0]);
|
||||
let parser_context = parser_context.with_additional_node(&contexts[1]);
|
||||
let parser_context = parser_context.with_additional_node(&contexts[2]);
|
||||
let element_matcher = parser_with_context!(element(true))(&parser_context);
|
||||
let exit_matcher = parser_with_context!(exit_matcher_parser)(&parser_context);
|
||||
let (mut remaining, (planning_element, property_drawer_element)) = tuple((
|
||||
opt(parser_with_context!(planning)(&parser_context)),
|
||||
opt(parser_with_context!(property_drawer)(&parser_context)),
|
||||
))(input)?;
|
||||
if planning_element.is_none() && property_drawer_element.is_none() {
|
||||
let (remain, _ws) = many0(blank_line)(remaining)?;
|
||||
remaining = remain;
|
||||
input = remain;
|
||||
}
|
||||
let (remaining, (mut children, _exit_contents)) = verify(
|
||||
many_till(element_matcher, exit_matcher),
|
||||
|(children, _exit_contents)| {
|
||||
!children.is_empty() || property_drawer_element.is_some() || planning_element.is_some()
|
||||
},
|
||||
)(remaining)?;
|
||||
property_drawer_element
|
||||
.map(Element::PropertyDrawer)
|
||||
.map(|ele| children.insert(0, ele));
|
||||
planning_element
|
||||
.map(Element::Planning)
|
||||
.map(|ele| children.insert(0, ele));
|
||||
|
||||
let (remaining, _trailing_ws) =
|
||||
maybe_consume_trailing_whitespace_if_not_exiting(context, remaining)?;
|
||||
|
||||
let source = get_consumed(input, remaining);
|
||||
Ok((
|
||||
remaining,
|
||||
Section {
|
||||
source: source.into(),
|
||||
children,
|
||||
},
|
||||
))
|
||||
}
|
||||
|
||||
#[cfg_attr(feature = "tracing", tracing::instrument(ret, level = "debug"))]
|
||||
fn section_end<'b, 'g, 'r, 's>(
|
||||
_context: RefContext<'b, 'g, 'r, 's>,
|
||||
input: OrgSource<'s>,
|
||||
) -> Res<OrgSource<'s>, OrgSource<'s>> {
|
||||
recognize(detect_headline)(input)
|
||||
}
|
||||
@@ -1,18 +1,20 @@
|
||||
use nom::branch::alt;
|
||||
use nom::bytes::complete::tag;
|
||||
use nom::combinator::opt;
|
||||
use nom::combinator::recognize;
|
||||
use nom::sequence::tuple;
|
||||
|
||||
use super::org_source::OrgSource;
|
||||
use super::util::get_consumed;
|
||||
use super::util::maybe_consume_object_trailing_whitespace_if_not_exiting;
|
||||
use super::Context;
|
||||
use crate::context::parser_with_context;
|
||||
use crate::context::RefContext;
|
||||
use crate::error::Res;
|
||||
use crate::parser::parser_with_context::parser_with_context;
|
||||
use crate::parser::StatisticsCookie;
|
||||
use crate::types::StatisticsCookie;
|
||||
|
||||
#[cfg_attr(feature = "tracing", tracing::instrument(ret, level = "debug"))]
|
||||
pub fn statistics_cookie<'r, 's>(
|
||||
context: Context<'r, 's>,
|
||||
pub fn statistics_cookie<'b, 'g, 'r, 's>(
|
||||
context: RefContext<'b, 'g, 'r, 's>,
|
||||
input: OrgSource<'s>,
|
||||
) -> Res<OrgSource<'s>, StatisticsCookie<'s>> {
|
||||
alt((
|
||||
@@ -22,14 +24,18 @@ pub fn statistics_cookie<'r, 's>(
|
||||
}
|
||||
|
||||
#[cfg_attr(feature = "tracing", tracing::instrument(ret, level = "debug"))]
|
||||
pub fn percent_statistics_cookie<'r, 's>(
|
||||
context: Context<'r, 's>,
|
||||
pub fn percent_statistics_cookie<'b, 'g, 'r, 's>(
|
||||
context: RefContext<'b, 'g, 'r, 's>,
|
||||
input: OrgSource<'s>,
|
||||
) -> Res<OrgSource<'s>, StatisticsCookie<'s>> {
|
||||
let (remaining, source) =
|
||||
recognize(tuple((tag("["), nom::character::complete::u64, tag("%]"))))(input)?;
|
||||
let (remaining, _) = recognize(tuple((
|
||||
tag("["),
|
||||
opt(nom::character::complete::u64),
|
||||
tag("%]"),
|
||||
)))(input)?;
|
||||
let (remaining, _trailing_whitespace) =
|
||||
maybe_consume_object_trailing_whitespace_if_not_exiting(context, remaining)?;
|
||||
let source = get_consumed(input, remaining);
|
||||
Ok((
|
||||
remaining,
|
||||
StatisticsCookie {
|
||||
@@ -39,19 +45,20 @@ pub fn percent_statistics_cookie<'r, 's>(
|
||||
}
|
||||
|
||||
#[cfg_attr(feature = "tracing", tracing::instrument(ret, level = "debug"))]
|
||||
pub fn fraction_statistics_cookie<'r, 's>(
|
||||
context: Context<'r, 's>,
|
||||
pub fn fraction_statistics_cookie<'b, 'g, 'r, 's>(
|
||||
context: RefContext<'b, 'g, 'r, 's>,
|
||||
input: OrgSource<'s>,
|
||||
) -> Res<OrgSource<'s>, StatisticsCookie<'s>> {
|
||||
let (remaining, source) = recognize(tuple((
|
||||
let (remaining, _) = recognize(tuple((
|
||||
tag("["),
|
||||
nom::character::complete::u64,
|
||||
opt(nom::character::complete::u64),
|
||||
tag("/"),
|
||||
nom::character::complete::u64,
|
||||
opt(nom::character::complete::u64),
|
||||
tag("]"),
|
||||
)))(input)?;
|
||||
let (remaining, _trailing_whitespace) =
|
||||
maybe_consume_object_trailing_whitespace_if_not_exiting(context, remaining)?;
|
||||
let source = get_consumed(input, remaining);
|
||||
Ok((
|
||||
remaining,
|
||||
StatisticsCookie {
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
use nom::branch::alt;
|
||||
use nom::bytes::complete::tag;
|
||||
use nom::bytes::complete::take_while;
|
||||
use nom::character::complete::anychar;
|
||||
use nom::character::complete::one_of;
|
||||
use nom::combinator::map;
|
||||
@@ -9,33 +10,49 @@ use nom::combinator::peek;
|
||||
use nom::combinator::recognize;
|
||||
use nom::combinator::verify;
|
||||
use nom::multi::many_till;
|
||||
use nom::sequence::tuple;
|
||||
|
||||
use super::object_parser::standard_set_object;
|
||||
use super::org_source::BracketDepth;
|
||||
use super::org_source::OrgSource;
|
||||
use super::util::exit_matcher_parser;
|
||||
use super::util::maybe_consume_object_trailing_whitespace_if_not_exiting;
|
||||
use super::Context;
|
||||
use super::Object;
|
||||
use super::util::preceded_by_whitespace;
|
||||
use crate::context::parser_with_context;
|
||||
use crate::context::ContextElement;
|
||||
use crate::context::ContextMatcher;
|
||||
use crate::context::ExitClass;
|
||||
use crate::context::ExitMatcherNode;
|
||||
use crate::context::RefContext;
|
||||
use crate::error::CustomError;
|
||||
use crate::error::MyError;
|
||||
use crate::error::Res;
|
||||
use crate::parser::exiting::ExitClass;
|
||||
use crate::parser::object_parser::standard_set_object;
|
||||
use crate::parser::parser_context::ContextElement;
|
||||
use crate::parser::parser_context::ExitMatcherNode;
|
||||
use crate::parser::parser_with_context::parser_with_context;
|
||||
use crate::parser::util::exit_matcher_parser;
|
||||
use crate::parser::util::get_consumed;
|
||||
use crate::parser::Subscript;
|
||||
use crate::parser::Superscript;
|
||||
use crate::types::Object;
|
||||
use crate::types::Subscript;
|
||||
use crate::types::Superscript;
|
||||
|
||||
#[cfg_attr(feature = "tracing", tracing::instrument(ret, level = "debug"))]
|
||||
pub fn subscript<'r, 's>(
|
||||
context: Context<'r, 's>,
|
||||
pub fn detect_subscript_or_superscript<'s>(input: OrgSource<'s>) -> Res<OrgSource<'s>, ()> {
|
||||
// This does not have to detect all valid subscript/superscript but all that it detects must be valid.
|
||||
let (remaining, _) = one_of("_^")(input)?;
|
||||
pre(input)?;
|
||||
if tag::<_, _, CustomError<_>>("*")(remaining).is_ok() {
|
||||
return Ok((input, ()));
|
||||
}
|
||||
let (remaining, _) = opt(one_of("+-"))(remaining)?;
|
||||
let (_remaining, _) = verify(anychar, |c| c.is_alphanumeric())(remaining)?;
|
||||
Ok((input, ()))
|
||||
}
|
||||
|
||||
#[cfg_attr(feature = "tracing", tracing::instrument(ret, level = "debug"))]
|
||||
pub fn subscript<'b, 'g, 'r, 's>(
|
||||
context: RefContext<'b, 'g, 'r, 's>,
|
||||
input: OrgSource<'s>,
|
||||
) -> Res<OrgSource<'s>, Subscript<'s>> {
|
||||
// We check for the underscore first before checking the pre-character as a minor optimization to avoid walking up the context tree to find the document root unnecessarily.
|
||||
let (remaining, _) = tag("_")(input)?;
|
||||
pre(context, input)?;
|
||||
pre(input)?;
|
||||
let (remaining, _body) = script_body(context, remaining)?;
|
||||
let (remaining, _trailing_whitespace) =
|
||||
maybe_consume_object_trailing_whitespace_if_not_exiting(context, remaining)?;
|
||||
@@ -49,13 +66,13 @@ pub fn subscript<'r, 's>(
|
||||
}
|
||||
|
||||
#[cfg_attr(feature = "tracing", tracing::instrument(ret, level = "debug"))]
|
||||
pub fn superscript<'r, 's>(
|
||||
context: Context<'r, 's>,
|
||||
pub fn superscript<'b, 'g, 'r, 's>(
|
||||
context: RefContext<'b, 'g, 'r, 's>,
|
||||
input: OrgSource<'s>,
|
||||
) -> Res<OrgSource<'s>, Superscript<'s>> {
|
||||
// We check for the circumflex first before checking the pre-character as a minor optimization to avoid walking up the context tree to find the document root unnecessarily.
|
||||
let (remaining, _) = tag("^")(input)?;
|
||||
pre(context, input)?;
|
||||
pre(input)?;
|
||||
let (remaining, _body) = script_body(context, remaining)?;
|
||||
let (remaining, _trailing_whitespace) =
|
||||
maybe_consume_object_trailing_whitespace_if_not_exiting(context, remaining)?;
|
||||
@@ -69,16 +86,8 @@ pub fn superscript<'r, 's>(
|
||||
}
|
||||
|
||||
#[cfg_attr(feature = "tracing", tracing::instrument(ret, level = "debug"))]
|
||||
fn pre<'r, 's>(_context: Context<'r, 's>, input: OrgSource<'s>) -> Res<OrgSource<'s>, ()> {
|
||||
let preceding_character = input.get_preceding_character();
|
||||
match preceding_character {
|
||||
Some(c) if !c.is_whitespace() => {}
|
||||
_ => {
|
||||
return Err(nom::Err::Error(CustomError::MyError(MyError(
|
||||
"Must be preceded by a non-whitespace character.".into(),
|
||||
))));
|
||||
}
|
||||
};
|
||||
fn pre<'s>(input: OrgSource<'s>) -> Res<OrgSource<'s>, ()> {
|
||||
not(preceded_by_whitespace(true))(input)?;
|
||||
Ok((input, ()))
|
||||
}
|
||||
|
||||
@@ -89,8 +98,8 @@ enum ScriptBody<'s> {
|
||||
}
|
||||
|
||||
#[cfg_attr(feature = "tracing", tracing::instrument(ret, level = "debug"))]
|
||||
fn script_body<'r, 's>(
|
||||
context: Context<'r, 's>,
|
||||
fn script_body<'b, 'g, 'r, 's>(
|
||||
context: RefContext<'b, 'g, 'r, 's>,
|
||||
input: OrgSource<'s>,
|
||||
) -> Res<OrgSource<'s>, ScriptBody<'s>> {
|
||||
alt((
|
||||
@@ -107,61 +116,54 @@ fn script_body<'r, 's>(
|
||||
}
|
||||
|
||||
#[cfg_attr(feature = "tracing", tracing::instrument(ret, level = "debug"))]
|
||||
fn script_asterisk<'r, 's>(
|
||||
_context: Context<'r, 's>,
|
||||
fn script_asterisk<'b, 'g, 'r, 's>(
|
||||
_context: RefContext<'b, 'g, 'r, 's>,
|
||||
input: OrgSource<'s>,
|
||||
) -> Res<OrgSource<'s>, OrgSource<'s>> {
|
||||
tag("*")(input)
|
||||
}
|
||||
|
||||
#[cfg_attr(feature = "tracing", tracing::instrument(ret, level = "debug"))]
|
||||
fn script_alphanum<'r, 's>(
|
||||
context: Context<'r, 's>,
|
||||
fn script_alphanum<'b, 'g, 'r, 's>(
|
||||
_context: RefContext<'b, 'g, 'r, 's>,
|
||||
input: OrgSource<'s>,
|
||||
) -> Res<OrgSource<'s>, OrgSource<'s>> {
|
||||
let (remaining, _sign) = opt(recognize(one_of("+-")))(input)?;
|
||||
let (remaining, _script) = many_till(
|
||||
parser_with_context!(script_alphanum_character)(context),
|
||||
parser_with_context!(end_script_alphanum_character)(context),
|
||||
)(remaining)?;
|
||||
let (remaining, _script) =
|
||||
many_till(script_alphanum_character, end_script_alphanum_character)(remaining)?;
|
||||
let source = get_consumed(input, remaining);
|
||||
Ok((remaining, source))
|
||||
}
|
||||
|
||||
#[cfg_attr(feature = "tracing", tracing::instrument(ret, level = "debug"))]
|
||||
fn script_alphanum_character<'r, 's>(
|
||||
_context: Context<'r, 's>,
|
||||
input: OrgSource<'s>,
|
||||
) -> Res<OrgSource<'s>, OrgSource<'s>> {
|
||||
fn script_alphanum_character<'s>(input: OrgSource<'s>) -> Res<OrgSource<'s>, OrgSource<'s>> {
|
||||
recognize(verify(anychar, |c| {
|
||||
c.is_alphanumeric() || r#",.\"#.contains(*c)
|
||||
}))(input)
|
||||
}
|
||||
|
||||
#[cfg_attr(feature = "tracing", tracing::instrument(ret, level = "debug"))]
|
||||
fn end_script_alphanum_character<'r, 's>(
|
||||
context: Context<'r, 's>,
|
||||
input: OrgSource<'s>,
|
||||
) -> Res<OrgSource<'s>, OrgSource<'s>> {
|
||||
fn end_script_alphanum_character<'s>(input: OrgSource<'s>) -> Res<OrgSource<'s>, OrgSource<'s>> {
|
||||
let (remaining, final_char) = recognize(verify(anychar, |c| c.is_alphanumeric()))(input)?;
|
||||
peek(not(parser_with_context!(script_alphanum_character)(
|
||||
context,
|
||||
peek(tuple((
|
||||
take_while(|c| r#",.\"#.contains(c)),
|
||||
not(script_alphanum_character),
|
||||
)))(remaining)?;
|
||||
Ok((remaining, final_char))
|
||||
}
|
||||
|
||||
#[cfg_attr(feature = "tracing", tracing::instrument(ret, level = "debug"))]
|
||||
fn script_with_braces<'r, 's>(
|
||||
context: Context<'r, 's>,
|
||||
fn script_with_braces<'b, 'g, 'r, 's>(
|
||||
context: RefContext<'b, 'g, 'r, 's>,
|
||||
input: OrgSource<'s>,
|
||||
) -> Res<OrgSource<'s>, Vec<Object<'s>>> {
|
||||
let (remaining, _) = tag("{")(input)?;
|
||||
let exit_with_depth = script_with_braces_end(remaining.get_brace_depth());
|
||||
let parser_context =
|
||||
context.with_additional_node(ContextElement::ExitMatcherNode(ExitMatcherNode {
|
||||
class: ExitClass::Gamma,
|
||||
exit_matcher: &exit_with_depth,
|
||||
}));
|
||||
let parser_context = ContextElement::ExitMatcherNode(ExitMatcherNode {
|
||||
class: ExitClass::Gamma,
|
||||
exit_matcher: &exit_with_depth,
|
||||
});
|
||||
let parser_context = context.with_additional_node(&parser_context);
|
||||
|
||||
let (remaining, (children, _exit_contents)) = many_till(
|
||||
parser_with_context!(standard_set_object)(&parser_context),
|
||||
@@ -172,17 +174,15 @@ fn script_with_braces<'r, 's>(
|
||||
Ok((remaining, children))
|
||||
}
|
||||
|
||||
fn script_with_braces_end(
|
||||
starting_brace_depth: BracketDepth,
|
||||
) -> impl for<'r, 's> Fn(Context<'r, 's>, OrgSource<'s>) -> Res<OrgSource<'s>, OrgSource<'s>> {
|
||||
move |context: Context, input: OrgSource<'_>| {
|
||||
fn script_with_braces_end(starting_brace_depth: BracketDepth) -> impl ContextMatcher {
|
||||
move |context, input: OrgSource<'_>| {
|
||||
_script_with_braces_end(context, input, starting_brace_depth)
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg_attr(feature = "tracing", tracing::instrument(ret, level = "debug"))]
|
||||
fn _script_with_braces_end<'r, 's>(
|
||||
_context: Context<'r, 's>,
|
||||
fn _script_with_braces_end<'b, 'g, 'r, 's>(
|
||||
_context: RefContext<'b, 'g, 'r, 's>,
|
||||
input: OrgSource<'s>,
|
||||
starting_brace_depth: BracketDepth,
|
||||
) -> Res<OrgSource<'s>, OrgSource<'s>> {
|
||||
|
||||
@@ -8,43 +8,49 @@ use nom::combinator::not;
|
||||
use nom::combinator::peek;
|
||||
use nom::combinator::recognize;
|
||||
use nom::combinator::verify;
|
||||
use nom::multi::many0;
|
||||
use nom::multi::many1;
|
||||
use nom::multi::many_till;
|
||||
use nom::sequence::tuple;
|
||||
|
||||
use super::keyword::table_formula_keyword;
|
||||
use super::object_parser::table_cell_set_object;
|
||||
use super::org_source::OrgSource;
|
||||
use super::Context;
|
||||
use super::util::exit_matcher_parser;
|
||||
use crate::context::parser_with_context;
|
||||
use crate::context::ContextElement;
|
||||
use crate::context::ExitClass;
|
||||
use crate::context::ExitMatcherNode;
|
||||
use crate::context::RefContext;
|
||||
use crate::error::Res;
|
||||
use crate::parser::exiting::ExitClass;
|
||||
use crate::parser::greater_element::TableRow;
|
||||
use crate::parser::lesser_element::TableCell;
|
||||
use crate::parser::parser_context::ContextElement;
|
||||
use crate::parser::parser_context::ExitMatcherNode;
|
||||
use crate::parser::parser_with_context::parser_with_context;
|
||||
use crate::parser::util::exit_matcher_parser;
|
||||
use crate::parser::util::get_consumed;
|
||||
use crate::parser::util::start_of_line;
|
||||
use crate::parser::Table;
|
||||
use crate::types::Table;
|
||||
use crate::types::TableCell;
|
||||
use crate::types::TableRow;
|
||||
|
||||
/// Parse an org-mode-style table
|
||||
///
|
||||
/// This is not the table.el style.
|
||||
#[cfg_attr(feature = "tracing", tracing::instrument(ret, level = "debug"))]
|
||||
pub fn org_mode_table<'r, 's>(
|
||||
context: Context<'r, 's>,
|
||||
pub fn org_mode_table<'b, 'g, 'r, 's>(
|
||||
context: RefContext<'b, 'g, 'r, 's>,
|
||||
input: OrgSource<'s>,
|
||||
) -> Res<OrgSource<'s>, Table<'s>> {
|
||||
start_of_line(input)?;
|
||||
peek(tuple((space0, tag("|"))))(input)?;
|
||||
|
||||
let parser_context = context
|
||||
.with_additional_node(ContextElement::ConsumeTrailingWhitespace(true))
|
||||
.with_additional_node(ContextElement::Context("table"))
|
||||
.with_additional_node(ContextElement::ExitMatcherNode(ExitMatcherNode {
|
||||
let contexts = [
|
||||
ContextElement::ConsumeTrailingWhitespace(true),
|
||||
ContextElement::Context("table"),
|
||||
ContextElement::ExitMatcherNode(ExitMatcherNode {
|
||||
class: ExitClass::Alpha,
|
||||
exit_matcher: &table_end,
|
||||
}));
|
||||
}),
|
||||
];
|
||||
let parser_context = context.with_additional_node(&contexts[0]);
|
||||
let parser_context = parser_context.with_additional_node(&contexts[1]);
|
||||
let parser_context = parser_context.with_additional_node(&contexts[2]);
|
||||
|
||||
let org_mode_table_row_matcher = parser_with_context!(org_mode_table_row)(&parser_context);
|
||||
let exit_matcher = parser_with_context!(exit_matcher_parser)(&parser_context);
|
||||
@@ -52,6 +58,9 @@ pub fn org_mode_table<'r, 's>(
|
||||
let (remaining, (children, _exit_contents)) =
|
||||
many_till(org_mode_table_row_matcher, exit_matcher)(input)?;
|
||||
|
||||
let (remaining, formulas) =
|
||||
many0(parser_with_context!(table_formula_keyword)(context))(remaining)?;
|
||||
|
||||
// TODO: Consume trailing formulas
|
||||
let source = get_consumed(input, remaining);
|
||||
|
||||
@@ -59,14 +68,15 @@ pub fn org_mode_table<'r, 's>(
|
||||
remaining,
|
||||
Table {
|
||||
source: source.into(),
|
||||
formulas,
|
||||
children,
|
||||
},
|
||||
))
|
||||
}
|
||||
|
||||
#[cfg_attr(feature = "tracing", tracing::instrument(ret, level = "debug"))]
|
||||
fn table_end<'r, 's>(
|
||||
_context: Context<'r, 's>,
|
||||
fn table_end<'b, 'g, 'r, 's>(
|
||||
_context: RefContext<'b, 'g, 'r, 's>,
|
||||
input: OrgSource<'s>,
|
||||
) -> Res<OrgSource<'s>, OrgSource<'s>> {
|
||||
start_of_line(input)?;
|
||||
@@ -74,8 +84,8 @@ fn table_end<'r, 's>(
|
||||
}
|
||||
|
||||
#[cfg_attr(feature = "tracing", tracing::instrument(ret, level = "debug"))]
|
||||
pub fn org_mode_table_row<'r, 's>(
|
||||
context: Context<'r, 's>,
|
||||
pub fn org_mode_table_row<'b, 'g, 'r, 's>(
|
||||
context: RefContext<'b, 'g, 'r, 's>,
|
||||
input: OrgSource<'s>,
|
||||
) -> Res<OrgSource<'s>, TableRow<'s>> {
|
||||
alt((
|
||||
@@ -85,8 +95,8 @@ pub fn org_mode_table_row<'r, 's>(
|
||||
}
|
||||
|
||||
#[cfg_attr(feature = "tracing", tracing::instrument(ret, level = "debug"))]
|
||||
pub fn org_mode_table_row_rule<'r, 's>(
|
||||
_context: Context<'r, 's>,
|
||||
pub fn org_mode_table_row_rule<'b, 'g, 'r, 's>(
|
||||
_context: RefContext<'b, 'g, 'r, 's>,
|
||||
input: OrgSource<'s>,
|
||||
) -> Res<OrgSource<'s>, TableRow<'s>> {
|
||||
start_of_line(input)?;
|
||||
@@ -102,8 +112,8 @@ pub fn org_mode_table_row_rule<'r, 's>(
|
||||
}
|
||||
|
||||
#[cfg_attr(feature = "tracing", tracing::instrument(ret, level = "debug"))]
|
||||
pub fn org_mode_table_row_regular<'r, 's>(
|
||||
context: Context<'r, 's>,
|
||||
pub fn org_mode_table_row_regular<'b, 'g, 'r, 's>(
|
||||
context: RefContext<'b, 'g, 'r, 's>,
|
||||
input: OrgSource<'s>,
|
||||
) -> Res<OrgSource<'s>, TableRow<'s>> {
|
||||
start_of_line(input)?;
|
||||
@@ -122,15 +132,15 @@ pub fn org_mode_table_row_regular<'r, 's>(
|
||||
}
|
||||
|
||||
#[cfg_attr(feature = "tracing", tracing::instrument(ret, level = "debug"))]
|
||||
pub fn org_mode_table_cell<'r, 's>(
|
||||
context: Context<'r, 's>,
|
||||
pub fn org_mode_table_cell<'b, 'g, 'r, 's>(
|
||||
context: RefContext<'b, 'g, 'r, 's>,
|
||||
input: OrgSource<'s>,
|
||||
) -> Res<OrgSource<'s>, TableCell<'s>> {
|
||||
let parser_context =
|
||||
context.with_additional_node(ContextElement::ExitMatcherNode(ExitMatcherNode {
|
||||
class: ExitClass::Beta,
|
||||
exit_matcher: &org_mode_table_cell_end,
|
||||
}));
|
||||
let parser_context = ContextElement::ExitMatcherNode(ExitMatcherNode {
|
||||
class: ExitClass::Beta,
|
||||
exit_matcher: &org_mode_table_cell_end,
|
||||
});
|
||||
let parser_context = context.with_additional_node(&parser_context);
|
||||
let table_cell_set_object_matcher =
|
||||
parser_with_context!(table_cell_set_object)(&parser_context);
|
||||
let exit_matcher = parser_with_context!(exit_matcher_parser)(&parser_context);
|
||||
@@ -155,8 +165,8 @@ pub fn org_mode_table_cell<'r, 's>(
|
||||
}
|
||||
|
||||
#[cfg_attr(feature = "tracing", tracing::instrument(ret, level = "debug"))]
|
||||
fn org_mode_table_cell_end<'r, 's>(
|
||||
_context: Context<'r, 's>,
|
||||
fn org_mode_table_cell_end<'b, 'g, 'r, 's>(
|
||||
_context: RefContext<'b, 'g, 'r, 's>,
|
||||
input: OrgSource<'s>,
|
||||
) -> Res<OrgSource<'s>, OrgSource<'s>> {
|
||||
recognize(tuple((space0, alt((tag("|"), peek(line_ending))))))(input)
|
||||
|
||||
Some files were not shown because too many files have changed in this diff Show More
Reference in New Issue
Block a user