How ruby ​​interpreter interprets double quotes

Background

I am using a Ruby - like language called Sapphire as a way to try out some of the ideas that I have on concurrency in programming languages. I am trying to copy double-quoted Ruby strings with embedded code, which I find very useful as a programmer.

Question

How does any Ruby interpreter include a double-quoted string with embedded code and AST?

eg:

puts "The value of foo is #{@foo}."

puts "this is an example of unmatched braces in code: #{ foo.go('}') }"

More details

The problem is how to decide which }code block closes. Blocks of code can have other braces inside them and with little effort they can be unsurpassed. A lexer can find the beginning of a code block in a string, but without the help of a parser, he cannot know exactly which character is the end of this block.

It looks like the Ruby file parse.yperforms both lexing and parsing steps, but reading this thing is a nightmare , it has 11,628 lines without comment and a lot of abbreviations.

+4
source share
5 answers

, Yacc , parse.y - , ? - ?

, , , ., , , SO. , , . #{ , , . , } , . ( , ;), } - , .

+2

, , , .

Matz yylex() parse.y 11 . , , lex_state Ruby.

.

+1

Dart , , Ruby, . , , , . , :

"before ${the + expression} after"

:

STRING_START "before "
IDENTIFIER   the
PLUS
IDENTIFIER   expression
STRING       " after"

STRING_START (), .

+1

, , ( AST ).

Ruby . . , , .

? , AST. () , , , , , .

, . , . .

: , .

+1

Ruby (. ) Ruby , , , , , , , , , Ruby , .

, ( Ruby, many) . () , . OP; , / .

, AST Ruby .

- Ruby. . https://softwarerecs.stackexchange.com/q/11779/101

0

Source: https://habr.com/ru/post/1524618/


All Articles