parser
This commit is contained in:
parent
41eeaa459d
commit
550fba6f0b
6 changed files with 93 additions and 22 deletions
21
LICENSE
21
LICENSE
|
@ -1,21 +0,0 @@
|
|||
MIT License
|
||||
|
||||
Copyright (c) 2025 Tan Kian-ting <chenjt30@gmail.com> and contributors
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
of this software and associated documentation files (the "Software"), to deal
|
||||
in the Software without restriction, including without limitation the rights
|
||||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
copies of the Software, and to permit persons to whom the Software is
|
||||
furnished to do so, subject to the following conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be included in all
|
||||
copies or substantial portions of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
SOFTWARE.
|
|
@ -3,6 +3,11 @@ uuid = "de6df24e-a306-4916-96f6-3ab6c7568c2f"
|
|||
authors = ["Tan Kian-ting <chenjt30@gmail.com> and contributors"]
|
||||
version = "1.0.0-DEV"
|
||||
|
||||
[deps]
|
||||
ArgParse = "c7e460c6-2fb9-53a9-8c5b-16f535851c63"
|
||||
ParserCombinator = "fae87a5f-d1ad-5cf0-8f61-c941e1580b46"
|
||||
libharu_jll = "d4e8948d-4b7e-5538-9d15-404f6f0a9070"
|
||||
|
||||
[compat]
|
||||
julia = "1.6.7"
|
||||
|
||||
|
|
7
example/ex1 copy.ug
Normal file
7
example/ex1 copy.ug
Normal file
|
@ -0,0 +1,7 @@
|
|||
%123%@foo text%456 7% %
|
||||
|
||||
8 9 10%
|
||||
|
||||
{@foo}
|
||||
|
||||
{@foo|@bar}
|
2
example/ex1.ug
Normal file
2
example/ex1.ug
Normal file
|
@ -0,0 +1,2 @@
|
|||
@foo
|
||||
{@foo|@bar|12\|}
|
52
src/parsing.jl
Normal file
52
src/parsing.jl
Normal file
|
@ -0,0 +1,52 @@
|
|||
module Parsing
|
||||
using ParserCombinator
|
||||
abstract type Node end
|
||||
struct ID<:Node val end
|
||||
struct SEQ<:Node val end # like (a b c) in scheme
|
||||
struct ELE<:Node val end #an element in a seq
|
||||
struct ESC_CHAR<:Node val end # character preceded by escape char "\"
|
||||
|
||||
#=
|
||||
grammar rules of uahgi
|
||||
=#
|
||||
|
||||
comment = p"\%[^%]+\%"
|
||||
newline = p"(\r?\n)"
|
||||
space = p"[ \t]"
|
||||
|
||||
id_name = p"[_a-zA-Z][_0-9a-zA-Z]*" > ID
|
||||
id = E"@" + id_name
|
||||
|
||||
char = p"[^ \n\r\t\\]"#[1:2,:?]
|
||||
|
||||
# chars should be preceded by "\" are \, {, }, |, @, %
|
||||
esc_char = p"[\{\|\}\@\%]" > ESC_CHAR
|
||||
esc_combined = E"\\" + esc_char
|
||||
#=
|
||||
seq = (foo x1 x2 " ")
|
||||
=> {@foo|x1|x2| }
|
||||
=#
|
||||
char_and_combined = char | esc_combined
|
||||
seq_item = id | Repeat(char_and_combined) |> ELE
|
||||
seq_item_rest = E"|" + seq_item
|
||||
seq_inner = seq_item + (seq_item_rest)[0:end] |> SEQ
|
||||
seq = E"{" + seq_inner + E"}"
|
||||
|
||||
|
||||
part = seq | comment | space | newline | id | char
|
||||
all = Repeat(part) + Eos()
|
||||
|
||||
function parse(input)
|
||||
print(input)
|
||||
b = parse_one(input, all)
|
||||
print("\n" * string(b) * "\n")
|
||||
|
||||
#print(parse_one(, Pattern(r".b.")))
|
||||
end
|
||||
# Write your package code here.
|
||||
#export dog
|
||||
|
||||
#dog = 1.2
|
||||
|
||||
|
||||
end
|
28
src/uahgi.jl
28
src/uahgi.jl
|
@ -1,5 +1,31 @@
|
|||
module uahgi
|
||||
include("./parsing.jl")
|
||||
using .Parsing
|
||||
using ArgParse
|
||||
|
||||
# Write your package code here.
|
||||
function parse_commandline()
|
||||
#= please see:
|
||||
https://carlobaldassi.github.io/ArgParse.jl/stable/
|
||||
=#
|
||||
s = ArgParseSettings()
|
||||
|
||||
@add_arg_table! s begin
|
||||
"FILE"
|
||||
help = "the file path to be converted."
|
||||
required = true
|
||||
end
|
||||
|
||||
return parse_args(s)
|
||||
end
|
||||
|
||||
|
||||
function main()
|
||||
parsed_args = parse_commandline()
|
||||
file_path = parsed_args["FILE"]
|
||||
file_content = open(f->read(f, String), file_path)
|
||||
Parsing.parse(file_content)
|
||||
end
|
||||
|
||||
main()
|
||||
end
|
||||
|
||||
|
|
Loading…
Reference in a new issue