ztangent / Julog.jl

A Julia package for Prolog-style logic programming.
Apache License 2.0
170 stars 11 forks source link

Parsing dot character Issue in `convert_prolog_to_julog` #21

Open hiiroo opened 1 year ago

hiiroo commented 1 year ago

Hey there! Recently I had an issue with Julog trying to parse,

using Julog
s = """state("minnesota","mn","st. paul",4076.0e+3,84.4e+3,32,"minneapolis","st. paul","duluth","Bloomington")."""
Julog.convert_prolog_to_julog(s)

which outputs;

[
"state(\"minnesota\",\"mn\",\"st <<= true", 
"paul\",4076.0e+3,84.4e+3,32,\"minneapolis\",\"st <<= true",
"paul\",\"duluth\",\"bloomington\") <<= true"
]

It seems the issue is caused by the . character in the string. However, using the original ' character for wrapping strings(such as using it as 'st. paul') also causes parsing issue that says;

Base.Meta.ParseError("character literal contains multiple characters")

where, convert_prolog_to_julog seems to parse correctly as follows;

state('alabama','al','montgomery',3894.0e+3,51.7e+3,22,'birmingham','mobile','montgomery','huntsville') <<= true

However, Meta.parse attempting to read ' literal seems to be the cause.

Thanks in advance.

ztangent commented 1 year ago

Interesting, thanks! I think the issue is probably that this regular expression doesn't handle the case where a period appears in the middle of a string, i.e. in "st. paul":

https://github.com/ztangent/Julog.jl/blob/679b3f1520cf81fa97e33732645b5d7e8b4724e3/src/parse.jl#L151-L152

There should be a way to change this to handle the case where a period appears within double quotation marks.

To handle the the issue with single quotation marks, I think maybe there needs to be another line here which replaces the single quotation marks with double quotation marks:

https://github.com/ztangent/Julog.jl/blob/679b3f1520cf81fa97e33732645b5d7e8b4724e3/src/parse.jl#L154-L158

I don't have tons of time to get to this in the near term, but if you'd like to make a PR and add some test cases to check that it's working, I'd be happy to review it!