Skip to content

Commit

Permalink
Generalized from_string
Browse files Browse the repository at this point in the history
  • Loading branch information
liyishuai committed Sep 21, 2024
1 parent d63ba70 commit b827ba8
Showing 1 changed file with 31 additions and 10 deletions.
41 changes: 31 additions & 10 deletions Lexer.v
Original file line number Diff line number Diff line change
Expand Up @@ -55,16 +55,37 @@ Definition lex__token : parser token :=

CoFixpoint TheEnd : buffer := Buf_cons (EOF tt) TheEnd.

Definition lexer (str : string) : option string + buffer :=
match Parser.parse (many lex__token) str with
| inl err => inl err
| inr (l, _) => inr (app_buf l TheEnd)
Open Scope buffer_scope.
CoFixpoint push_back (b: buffer) (l: list token) : buffer :=
match b with
| EOF _ :: _ => l ++ TheEnd
| head :: tail => head :: push_back tail l
end.

Definition from_string (str : string) : option string + json :=
match lexer str with
| inl err => inl err
| inr b => if parse_json bigNumber b is Parsed_pr j _
then inr j
else inl (Some "Passed lexer but failed JSON parser")
Definition lexer' (acc: buffer * list ascii) (str: string)
: string + buffer * list ascii :=
let (buf, lchar) := acc in
match parse' (many lex__token) lchar str with

Check failure on line 68 in Lexer.v

View workflow job for this annotation

GitHub Actions / build (coqorg/coq:8.14)

The reference parse' was not found in the current environment.

Check failure on line 68 in Lexer.v

View workflow job for this annotation

GitHub Actions / build (coqorg/coq:8.15)

The reference parse' was not found in the current environment.

Check failure on line 68 in Lexer.v

View workflow job for this annotation

GitHub Actions / build (coqorg/coq:8.16)

The reference parse' was not found in the current environment.

Check failure on line 68 in Lexer.v

View workflow job for this annotation

GitHub Actions / build (coqorg/coq:8.17)

The reference parse' was not found in the current environment.

Check failure on line 68 in Lexer.v

View workflow job for this annotation

GitHub Actions / build (coqorg/coq:8.18)

The reference parse' was not found in the current environment.

Check failure on line 68 in Lexer.v

View workflow job for this annotation

GitHub Actions / build (coqorg/coq:8.19)

The reference parse' was not found in the current environment.

Check failure on line 68 in Lexer.v

View workflow job for this annotation

GitHub Actions / build (coqorg/coq:8.20)

The reference parse' was not found in the current environment.

Check failure on line 68 in Lexer.v

View workflow job for this annotation

GitHub Actions / build (coqorg/coq:dev)

The reference parse' was not found in the current environment.
| inl (Some err) => inl err
| inl None => inr acc
| inr (l, remainder) => inr (push_back buf l, remainder)
end.

Definition from_string' (acc: buffer * list ascii) (str: string)
: string + option json * buffer * list ascii :=
match lexer' acc str with
| inl e => inl e
| inr (b, remainder) =>
match parse_json bigNumber b with
| Parsed_pr j b' => inr (Some j, b', remainder)
| Fail_pr_full _ (EOF _) => inr (None , b , remainder)
| _ => inl "Passed lexer but failed JSON parser"
end
end.

Definition from_string (str: string) : option string + json :=
match from_string' (TheEnd, []) str with
| inl e => inl (Some e)
| inr (None , _, _) => inl None
| inr (Some j, _, _) => inr j
end.

0 comments on commit b827ba8

Please sign in to comment.