Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Port to cljc #3

Merged
merged 19 commits into from
Oct 9, 2019
Merged

Port to cljc #3

merged 19 commits into from
Oct 9, 2019

Conversation

PEZ
Copy link
Contributor

@PEZ PEZ commented Oct 6, 2019

Hello,

This is work In progress. Wanted to get your feedback on wether you think it is the right way to go or not. For me it is important to be able to use a library like this from ClojureScript, because that's what Calva needs.

Right now do not expect anything to really work. Running the node tests fails like so:

$ node target/out/tests.js 
/Users/pez/Projects/parcera/target/out/cljs/core.js:32980
return (new RegExp(pattern,(function (){var or__7908__auto__ = flags;
        ^

SyntaxError: Invalid regular expression: /\P{M}\p{M}*+/: Nothing to repeat
    at new RegExp (<anonymous>)
    at cljs$core$re_pattern (/Users/pez/Projects/parcera/target/out/cljs/core.js:32980:9)
    at instaparse$cfg$process_regexp (/Users/pez/Projects/parcera/target/out/instaparse/cfg.js:143:41)
    at instaparse$cfg$build_rule (/Users/pez/Projects/parcera/target/out/instaparse/cfg.js:196:85)
    at /Users/pez/Projects/parcera/target/out/cljs/core.js:17671:89
    at /Users/pez/Projects/parcera/target/out/cljs/core.js:17672:3
    at Object.sval (/Users/pez/Projects/parcera/target/out/cljs/core.js:11499:109)
    at Object.cljs$core$ISeqable$_seq$arity$1 (/Users/pez/Projects/parcera/target/out/cljs/core.js:11650:10)
    at Object.cljs$core$_seq [as _seq] (/Users/pez/Projects/parcera/target/out/cljs/core.js:2424:10)
    at Object.cljs$core$seq [as seq] (/Users/pez/Projects/parcera/target/out/cljs/core.js:4423:18)

@carocad
Copy link
Owner

carocad commented Oct 6, 2019

@PEZ thanks a lot for the effort. I am not very proficient with the Cljs side but I would guess that the StringBuffer implementation that @borkdude proposed would be enough for this use case. See #2 . From what I see both "classes" provide an append and a toString method which I guess should be enough to get this done.

SyntaxError: Invalid regular expression: /\P{M}\p{M}*+/: Nothing to repeat

Regarding the regular expression. That is a special (multicharacter) unicode regex. It might be that Javascript doesnt support that exact syntax so you would need to check that. I put the link from the regex next to the grammar definition for specific rule: https://github.com/carocad/parcera/blob/master/src/parcera/core.clj#L138

Hope it helps

@PEZ
Copy link
Contributor Author

PEZ commented Oct 6, 2019

Yeah, maybe we can use that StringBuffer instead. Even if the tiny StringBuilder I used makes for fewer changes in the rest of the code. I.e.something like:

(let [string-builder (new StringBuilder)]
  ... 

Stays the same, no need for the conditional reader there. It's a matter of taste, I think. (Though, I say that without having had a look at goop's implementation.)

Will have a look at the unicode regex thing. I've fought that fight before. Seems to vary a bit between node versions what is supported and not...

@PEZ
Copy link
Contributor Author

PEZ commented Oct 6, 2019

It seems that the current regex doesn't work for anything unicode as it is (regardless of CLJ or CLJS). Just sticking a Swedish ö in the backticks test:

  (testing "backtick"
    (as-> "`hellö/world" input (is (= input (parcera/code (parcera/clojure input)))))
    (as-> "`hello" input (is (= input (parcera/code (parcera/clojure input)))))
    (as-> "`/" input (is (= input (parcera/code (parcera/clojure input))))))

I get this:

ERROR in (macros) (core.cljc:159)
backtick
expected: (= input (parcera/code (parcera/clojure input)))
  actual: java.lang.IllegalArgumentException: No matching clause: [:index 5]

@carocad
Copy link
Owner

carocad commented Oct 6, 2019

It seems like you just caught a bug 😅. I was using autogenerated symbols to test the symbol-regex but it seems that it doesnt create symbols with unicode characters in it.

However, the regex that I mentioned was only for the character literals not for symbols which is why you just saw this behavior.

Also add test for character literals
@PEZ
Copy link
Contributor Author

PEZ commented Oct 6, 2019

I see. I think I just assumed that the simple characters built up the identifiers. Should have read more carefully.

Anyway. I've used http://kourge.net/projects/regexp-unicode-block to cook the character classes for the regex, since JavaScript just does not have the unicode support in its regex engine. Added a test for character literals, including unicode ones, which passes in both Clojure and ClojureScript. What'ya say?

@PEZ
Copy link
Contributor Author

PEZ commented Oct 6, 2019

Right now the test suite as such bails on the (slurp), which does not exist in CLJS. Otherwise everything that passes in CLJ passes in CLJS too.

@carocad
Copy link
Owner

carocad commented Oct 6, 2019

Right now the test suite as such bails on the (slurp), which does not exist in CLJS.

I guess you can make a macro that puts the result of slurp into a var that can later on be used for the tests. That way the slurp process is at compile time (clojure) not runtime cljs.

@PEZ
Copy link
Contributor Author

PEZ commented Oct 6, 2019

Right, did that now.

lein test
Warning: implicit hook found: leiningen.cljsbuild/activate 
Hooks are deprecated and will be removed in a future version.
Compiling ClojureScript...
Compiling ["target/out/tests.js"] from ["src" "test"]...
Successfully compiled ["target/out/tests.js"] in 6.827 seconds.
Compiling ClojureScript...

lein test parcera.test.core
"Elapsed time: 12232.174756 msecs"
"Elapsed time: 6812.244218 msecs"

Ran 5 tests containing 52 assertions.
0 failures, 0 errors.
Running ClojureScript test: dev

Testing parcera.test.core
"Elapsed time: 26574.280114 msecs"
"Elapsed time: 14582.774624 msecs"

Ran 5 tests containing 52 assertions.
0 failures, 0 errors.

The ClojureScript tests run significantly slower. Maybe that is what It is, but also maybe my naive StringBuilder isn't fast enough. (Can't see why it wouldn't be though.)

@carocad
Copy link
Owner

carocad commented Oct 7, 2019

The ClojureScript tests run significantly slower

I guess one part of it is due to the use of instaparse/parser instead of instaparse/defparaser as mentioned in the docs.

I think another thing that we can check are instaparse tests itself. I would assume that it also has some cljs tests with the same content as those in clj so if those are also significantly slower then there is little we can do :/

@PEZ
Copy link
Contributor Author

PEZ commented Oct 7, 2019

After checking up the StringBuffer implementation, and confirmed that it has the same performance as my own tiny shim, I decided that it is worth using the goog implementation instead.

I'll confirm that I can use this library from Calva (shadow-cljs) and if that works, I'd say this is good to go.

it will be easier for more people to obsess on the performance side of things if it gets merged. 😄

project.clj Outdated Show resolved Hide resolved
src/parcera/core.cljc Outdated Show resolved Hide resolved
project.clj Outdated Show resolved Hide resolved
Document the character literal unicode regex some.
Also moved up criterium in global dependencies.
I don't like it, but don't know how to otherwise
satsify the production build
@PEZ PEZ changed the title [WIP] Start porting to cljc Port to cljc Oct 7, 2019
project.clj Outdated Show resolved Hide resolved
@carocad
Copy link
Owner

carocad commented Oct 9, 2019

@PEZ sorry for creating the merge conflicts between this PR and the current master. As you know parcera is still in its infancy and I want to get it as correct as possible in the meantime.

By the way, #12 should help you run the tests directly on Travis CI. Hope it helps

@PEZ
Copy link
Contributor Author

PEZ commented Oct 9, 2019

I am happy to keep merging your changes from upstream! No worries.

And the JS tests are ... PASSING! Yay!

Copy link
Owner

@carocad carocad left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks a lot for this improvement :)

@carocad carocad merged commit 91f911c into carocad:master Oct 9, 2019
@PEZ PEZ deleted the wip/cljc branch October 9, 2019 22:30
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants