ptal / oak

A typed parser generator embedded in Rust code for Parsing Expression Grammars
Apache License 2.0
142 stars 14 forks source link

Parse string literal to Vec<char> #106

Closed jeizsm closed 6 years ago

jeizsm commented 6 years ago

Why string literal parsed to -> (^)? It makes no sense. If I want use string literal I need to look ahead with &"literal" and after get tuple of chars with same length and write a function to convert tuple of chars to vec or string or whatever. But if string literal return a value it can be easy dropped with -> (^) or maybe add new operator which can look ahead and consume value.

I tried realise it, but I don't really understand how oak works.

Sorry for my bad english.

ptal commented 6 years ago

Usually you do not want to retrieve the value of a string literal because they are purely syntactic entities. For example:

loop_stmt = "while" bool_expr "{" body "}"

You are only interested by the boolean condition of the while statement and its body. You don't care about the keyword "while" or the braces "{" and "}" because they are just there for the programmer.

If you really want to recover the value of a string literal, then you can just add it into you action rule:

kw_while = "while" > make_while

fn make_while() -> String {
  String::new("while");
}

But this is clearly weird if you are designing a parser for a programming language. Could you provide an example where this is useful?

jeizsm commented 6 years ago

I design parser for flatbuffers grammar and my original intent was just copy to oak type = bool | byte | ubyte | short | ushort | int | uint | float | long | ulong | double | int8 | uint8 | int16 | uint16 | int32 | uint32| int64 | uint64 | float32 | float64 | string | [ type ] | ident, but now I think about it and it really weird. I can omit all the types, just parse it like a ident and check types later. And in another examples I don't need string in the first place. Thank you :)

ptal commented 6 years ago

Glad you could figure it out! Best luck for your project!