4 ms·
You don’t need escaping in «This is a string containing an «embedded» quoted string».
by hk__2 9y ago
You don’t need escaping in «This is a string containing an «embedded» quoted string».
- drdeca 9y agoI think it would make the parsing a tiny bit more inconvenient though
- Pxtl 9y agoYup, suddenly your parser needs to keep a count of how many nested strings deep it is.
- jrochkind1 9y agoruby does that. computers are pretty fast now.
- userbinator 9y agoDon't forget that "parser" also includes human brains, which tend to not be that great at parsing nested things. To use formal language theory, strings containing escape characters are regular, i.e. parseable with a finite-state machine. Allowing nesting means you need a stack to find the matching pair.
- munificent 9y agoIt's not hard. Obviously the parser has to do that things that aren't a single token like parenthesized expressions. Even for things where the nesting does happen during lexical analysis, it's pretty trivial to keep a count in your lexer. Lots of languages support nesting comment syntax or string interpolation, which both have equivalent difficulty.
- hk__2 9y agoIt’s trivial to do; parsers handle nested things quite easily.
- tedunangst 9y agoDebatable whether that would actually work in practice. /* nested /* comments */ don't work */
- int_19h 9y agoThey don't work in C, but that's an arbitrary decision made by its designers. There are many languages that have balanced comments that can be nested. In OCaml, for example, this is legal: (* nested (* comments *) work *)
- jraph 9y agoThis is possible in D using /+ comments +/. https://wiki.dlang.org/Commenting_out_code#Nested_comments https://wiki.dlang.org/Commenting_out_code#Nested_comments This allows commenting out code containing comments, which can be useful when debugging or giving usage examples in the code.
- Someone 9y agoSo, how would you encode this in a string: To end a string, use the » character. ?
- hk__2 9y agoYou escape it. What I was saying was nested strings wouldn’t need escaping.
- cassowary 9y ago«Hi. How do I open a quote?» «Oh, you just use the « character.» Parse error. Unexpected EOF.
- hk__2 9y agoThat wouldn’t be a good idea but you could adapt your parser to support that case. You can write `/* /* */` in C, for instance. To clarify: we’d still need escaping but in fewer cases.