chris613
2019-9-16 14:14:40

i think maybe ive started down a bad / wrong path ….. if anyone has the time & all round lovelyness to make sure im not going completely off the reservation ….

i want to be able to javascript src code a text file and pull out certain parts. specifically i want to try and captue “test name” from the common it('xxxx' and describe("xxxx" style of test definitions.


chris613
2019-9-16 14:14:58

so i figred parsers were the right way to go?


chris613
2019-9-16 14:15:35

however because i want to ignore an awful lot of the inbetweeen parts, i seem to be running up against a lot of friction with the approach


chris613
2019-9-16 14:16:34

im hoping someone can just sanity check me and let me know if there is a vastly simpler approach or just validate my thinking that parsers are probably the correct solution, and stick with it you will get there sort of thing ? :slightly_smiling_face:


chris613
2019-9-16 14:16:38

thanks in advance


soegaard2
2019-9-16 14:18:34

Did you write your own parser for JavaScript?


chris613
2019-9-16 14:22:12

no, i dont want to parse the whole of the language


chris613
2019-9-16 14:22:31

eventually id like the approach to me src language agnostic


chris613
2019-9-16 14:23:06

so im trying to impliment the parser as small as possible and focused only on extracting the infomation i need. right now thats just test names


soegaard2
2019-9-16 14:24:18

How general do you need it? Would a line-by-line extraction with regular expressions be enough?


chris613
2019-9-16 14:26:57

mmmm possibly….


chris613
2019-9-16 14:27:49

possibly this is just over engineering, but that felt like a less robust solution ?


chris613
2019-9-16 14:28:47

the parser approach would be more likely to end up simpler when dealing with nested structures etc


chris613
2019-9-16 14:28:57

describe('..... it('.....) )


soegaard2
2019-9-16 14:31:20

I think you could write a 95% solution using regexps. An alternative is to use the lexical scanner from Herman’s JavaScript compiler. I don’t think you need the full parser. Simply turn the file into a stream of tokens. When you see “describe” "(" <string> you output the string. https://docs.racket-lang.org/javascript/parse.html#%28part._lexer%29


chris613
2019-9-16 14:32:11

yeah im playing about with lexers right now (after starting with parser from megaparsack)


chris613
2019-9-16 14:32:34

im not sure i entirely get the fundamental difference


soegaard2
2019-9-16 14:32:50

Between?


chris613
2019-9-16 14:33:25

lexers & parsers


chris613
2019-9-16 14:33:34

probably one for me to jsut google myself :slightly_smiling_face:


soegaard2
2019-9-16 14:34:36

The lexer breaks the source into small logical units like “number”, “string” “parenthesis” etc. No attempt of finding a structure is made.


soegaard2
2019-9-16 14:36:21

Another approach: Can you define your own version of “describe” and “it” in JavaScript. Which won’t run the tests, but simply print their names. Then run the tests with your modified describe and it.


chris613
2019-9-16 14:36:57

chris613
2019-9-16 14:38:00

i suppose that’s an option, but it sort of closes the door for other languages unless i can go a similar route of comming up with an interpriter / compiler & monkey patches for the test functions / classes etc


soegaard2
2019-9-16 14:38:51

I was thinking of using, say, node to run the JavaScript. But you are right, that it is a JavaScript only approach.


chris613
2019-9-16 14:38:58

sorra gotta run, but ill check back in later :slightly_smiling_face: really appreciate the advise / sounding board :slightly_smiling_face:


soegaard2
2019-9-16 14:57:09

@chris613 Personally I would write the regexp-solution first. Then replace it if it turned out not to be good enough.


stefan.kruger
2019-9-16 15:21:24

Can I use for/or to pick out the first element in a list for which some predicate is true? Like (for/or ([i '(1 3 5 7 8 9)]) (even? i))


stefan.kruger
2019-9-16 15:21:37

but what I like is the actual value, not #t/#f.


soegaard2
2019-9-16 15:22:11

(for/or ([i ’(1 3 5 7 8 9)]) (and (even? i) i)


soegaard2
2019-9-16 15:22:17

Or use #:when


samth
2019-9-16 15:22:49
(for/first ([i '(1 3 5 6 9)] #:when (even? i)) i)

stefan.kruger
2019-9-16 15:24:58

Nice. Reading the docs for for/or and for/first I find it hard to tell quite what the difference is between them.


soegaard2
2019-9-16 15:25:54

for/or continues when it sees #f. for/first will return #f


soegaard2
2019-9-16 15:26:48

So for/or returns the first non-false value and for/first returns the first value.


stefan.kruger
2019-9-16 15:28:38

So comparing the two approaches above, are they equivalent in terms of the number of invocations to even??


soegaard2
2019-9-16 15:29:38

I believe so, but you can replace (even? i) with (begin (display i) (even? i)) to check.


stefan.kruger
2019-9-16 15:32:55

Yes, they are.