Upgrade to Pro — share decks privately, control downloads, hide ads and more …

RNode with code locations

yui-knk
June 01, 2018

RNode with code locations

yui-knk

June 01, 2018
Tweet

More Decks by yui-knk

Other Decks in Programming

Transcript

  1. Self-introduction • Yuichiro Kaneko • Asakusa.rb • A CRuby Committer

    (2015/12~) • GitHub (yui-knk) • Twitter (spikeolaf)
  2. Today's topic • RNode • Node of Abstract Syntax Tree

    • Code location • Location information of RNode
  3. Run the code • Run the code on … •

    Ruby 2.4 • Ruby 2.5 $ ruby --dump=p -e '"str".upcase'
  4. Ruby 2.4 $ ruby --dump=p -e '"str".upcase' # @ NODE_SCOPE

    (line: 1) # +- nd_tbl: (empty) # +- nd_args: # | (null node) # +- nd_body: # @ NODE_PRELUDE (line: 1) # +- nd_head: # | (null node) # +- nd_body: # | @ NODE_CALL (line: 1) # | +- nd_mid: :upcase # | +- nd_recv: # | | @ NODE_STR (line: 1) # | | +- nd_lit: "str" # | +- nd_args: # | (null node) # +- nd_compile_option: # +- coverage_enabled: false
  5. $ ruby --dump=p -e '"str".upcase' # @ NODE_SCOPE (line: 1,

    code_range: (1,0)-(1,12)) # +- nd_tbl: (empty) # +- nd_args: # | (null node) # +- nd_body: # @ NODE_PRELUDE (line: 1, code_range: (1,0)-(1,12)) # +- nd_head: # | (null node) # +- nd_body: # | @ NODE_CALL (line: 1, code_range: (1,0)-(1,12)) # | +- nd_mid: :upcase # | +- nd_recv: # | | @ NODE_STR (line: 1, code_range: (1,0)-(1,5)) # | | +- nd_lit: "str" # | +- nd_args: # | (null node) # +- nd_compile_option: # +- coverage_enabled: false Ruby 2.5
  6. @@ -1,8 +1,8 @@ # +- nd_body: -# | @

    NODE_CALL (line: 1) +# | @ NODE_CALL (line: 1, code_range: (1,0)- (1,12)) # | +- nd_mid: :upcase # | +- nd_recv: -# | | @ NODE_STR (line: 1) +# | | @ NODE_STR (line: 1, code_range: (1,0)-(1,5)) # | | +- nd_lit: "str" # | +- nd_args: # | (null node)
  7. @@ -1,8 +1,8 @@ # +- nd_body: -# | @

    NODE_CALL (line: 1) +# | @ NODE_CALL (line: 1, code_range: (1,0)- (1,12)) # | +- nd_mid: :upcase # | +- nd_recv: -# | | @ NODE_STR (line: 1) +# | | @ NODE_STR (line: 1, code_range: (1,0)-(1,5)) # | | +- nd_lit: "str" # | +- nd_args: # | (null node) Today’s Topic
  8. Agenda • What code locations are • Why code locations

    are needed • Ruby crash course • How to implement code locations • The future plan of code locations feature • Conclusion
  9. Exception Traceback (most recent call last): 1: from src/exception.rb:5:in `<main>'

    src/exception.rb:2:in `a': undefined method `foo' for "":String (NoMethodError)
  10. Location information in programming • Location information of script is

    used in various situations. • "An exception is raised from line number XX" (Exception) • "Instance variable of line number XX not initialized" (Warning) • "No test cases for line number XX" (Coverage)
  11. Location and position • Location is presented by 2 numbers:

    • Line number (lineno) • Distance from beginning of line (column)
  12. Location and position • 4 numbers are needed to represent

    “begin” and “end”. • "Code position" is a pair of lineno and column. • "Code location" is a pair of begin position and end position. 1 + 2 ^ ^ ^ | | +- @3 (1.4-1.5) | +--- @2 (1.2-1.3) +----- @1 (1.0-1.1) @3 (1.4-1.5) Code position (begin) Code location @3 (1.4-1.5) Lineno (1) Column (4) Code position (end)
  13. Location in Ruby • Ruby holds *only* line numbers until

    Ruby 2.4. • Ruby holds line numbers and columns since Ruby 2.5. • Today’s main topic is “Column”.
  14. Minor details about column • 0-based / 1-based • 0-based

    • Vary according to programming languages and editors. • From the beginning of line / file • Line
  15. Minor details about column • Byte length / Character length

    • Byte length • “ߏจ໦ʹৄࡉͳҐஔ৘ใΛ΋ͨͤΔܭը” • https://bugs.ruby-lang.org/projects/ruby-trunk/wiki/ Node-position-memo
  16. For coverage features • For branch coverage and method coverage

    (Ruby 2.5~). • "An introduction and future of Ruby coverage library” • http://rubykaigi.org/2017/presentations/mametter.html (30:50-)
  17. What is branch coverage • "Branch coverage tells you which

    branches are executed, and which not." (doc/NEWS-2.5.0) (a == 2) ? :t : :f
  18. What is branch coverage • You may forget to write

    test codes for `then` cases. • `n/m` • `n`: How many times the “then clause” is executed. • `m`: How many times the “else clause” is executed. 0/1: (a == 2) ? :t : :f
  19. Use-case (1) • Code locations can be used for visualizing

    branch coverage results. 0/1: (a == 2) ? :t : :f
  20. Use-case (1) • Code locations can be used for visualizing

    branch coverage results. 0/1: (a == 2) ? :t : :f YOU SHOULD WRITE TEST !!!
  21. Use-case (2) • One line can contain one or more

    branches. • In these case, we can't recognize which clause is executed by only line numbers. (a == b) ? ((c == d) ? :A : :B) : :C obj&.foo? ? "a" : "b"
  22. Use-case (2) • One line can contain one or more

    branches. • In these case, we can't recognize which clause is executed by only line numbers. (a == b) ? ((c == d) ? :A : :B) : :C obj&.foo? ? "a" : "b"
  23. How Ruby script is processed 4UFQ *OQVU 0VUQVU %FCVH 4PSVDF

    5PLFOJ[BUJPO 3VCZTDSJQU 5PLFOT EVNQZ QBSTFZ 1BSTJOH 5PLFOT "45 EVNQQ QBSTFZ $PNQJMF "45 #ZUFDPEF EVNQJ DPNQJMFD Parsing ___ \ Ruby script -> Tokens -> AST -> Byte code (insns / ISeq) __/ __/ Tokenization Compile
  24. Tokenization Parsing ___ \ Ruby script -> Tokens -> AST

    -> Byte code (insns / ISeq) __/ __/ Tokenization Compile
  25. Tokenization • Each token has • a token type (tINTEGER)

    • a semantic value (1) 1 + 2 ^ ^ ^^ | | |+--- '\n' / "end-of-input" | | +---- tINTEGER (2) | +------ '+' +-------- tINTEGER (1)
  26. Tokenization $ ruby --dump=y -e '1 + 2' | grep

    Shifting Shifting token tINTEGER (1.0-1.1: ) Shifting token '+' (1.2-1.3: ) Shifting token tINTEGER (1.4-1.5: ) Shifting token '\n' (1.5-1.5: ) Shifting token "end-of-input" (1.5-1.5: ) On Ruby 2.5.1 1 2
  27. Tokenization $ ruby --dump=y -e '1 + 2' | grep

    Shifting On Ruby 2.6.0preview1 Shifting token tINTEGER (1.0-1.1: 1) Shifting token '+' (1.2-1.3: ) Shifting token tINTEGER (1.4-1.5: 2) Shifting token '\n' (1.5-1.5: ) Shifting token "end-of-input" (1.5-1.5: )
  28. Tokenization $ ruby --dump=y -e '1 + 2' | grep

    Shifting On Ruby 2.6.0preview1 Shifting token tINTEGER (1.0-1.1: 1) Shifting token '+' (1.2-1.3: ) Shifting token tINTEGER (1.4-1.5: 2) Shifting token '\n' (1.5-1.5: ) Shifting token "end-of-input" (1.5-1.5: )
  29. Parsing Parsing ___ \ Ruby script -> Tokens -> AST

    -> Byte code (insns / ISeq) __/ __/ Tokenization Compile
  30. Parsing numeric : simple_numeric | tUMINUS_NUM simple_numeric %prec tLOWEST ;

    simple_numeric : tINTEGER | tFLOAT | tRATIONAL | tIMAGINARY ; %% program : { } top_compstmt parse.y
  31. Parsing numeric : simple_numeric | tUMINUS_NUM simple_numeric %prec tLOWEST ;

    simple_numeric : tINTEGER | tFLOAT | tRATIONAL | tIMAGINARY ; %% program : { } top_compstmt W W W Rules parse.y
  32. Parsing numeric : simple_numeric | tUMINUS_NUM simple_numeric %prec tLOWEST ;

    simple_numeric : tINTEGER | tFLOAT | tRATIONAL | tIMAGINARY ; %% program : { } top_compstmt parse.y
  33. Parsing numeric : simple_numeric | tUMINUS_NUM simple_numeric %prec tLOWEST ;

    simple_numeric : tINTEGER | tFLOAT | tRATIONAL | tIMAGINARY ; %% program : { } top_compstmt 1 2.1 3r 4i parse.y
  34. Parsing numeric : simple_numeric | tUMINUS_NUM simple_numeric %prec tLOWEST ;

    simple_numeric : tINTEGER | tFLOAT | tRATIONAL | tIMAGINARY ; %% program : { } top_compstmt parse.y
  35. Parsing numeric : simple_numeric | tUMINUS_NUM simple_numeric %prec tLOWEST ;

    simple_numeric : tINTEGER | tFLOAT | tRATIONAL | tIMAGINARY ; %% program : { } top_compstmt 1 parse.y
  36. Parsing numeric : simple_numeric | tUMINUS_NUM simple_numeric %prec tLOWEST ;

    simple_numeric : tINTEGER | tFLOAT | tRATIONAL | tIMAGINARY ; %% program : { } top_compstmt 1 parse.y
  37. Parsing numeric : simple_numeric | tUMINUS_NUM simple_numeric %prec tLOWEST ;

    simple_numeric : tINTEGER | tFLOAT | tRATIONAL | tIMAGINARY ; %% program : { } top_compstmt Goal parse.y
  38. Parsing $ ruby --dump=y -e '1 + 2' # Shifting

    token tINTEGER (1) tINTEGER simple_numeric numeric literal primary arg
  39. Parsing # Shifting token '+' arg '+' # Shifting token

    tINTEGER (2) arg '+' tINTEGER arg '+' simple_numeric arg '+' numeric arg '+' literal arg '+' primary arg '+' arg arg expr stmt top_stmt top_stmts
  40. Parsing # Shifting token '\n' top_stmts '\n' top_stmts term top_stmts

    terms top_stmts opt_terms top_compstmt program # Completed
  41. Build AST $ ruby --dump=p -e '1 + 2' NODE_SCOPE

    NODE_PRELUDE NODE_OPCALL (:+) NODE_LIT (1) NODE_ARRAY NODE_LIT (2) NODE_SCOPE NODE_PRELUDE NODE_OPCALL (:+) NODE_LIT (1) NODE_ARRAY NODE_LIT (2)
  42. Build AST typedef struct RNode { VALUE flags; union {

    struct RNode *node; ... } u1; union { struct RNode *node; ... } u2; union { struct RNode *node; ... } u3; rb_code_location_t nd_loc; } NODE;
  43. Build AST typedef struct RNode { VALUE flags; union {

    struct RNode *node; ... } u1; union { struct RNode *node; ... } u2; union { struct RNode *node; ... } u3; rb_code_location_t nd_loc; } NODE; Contain node_type
  44. Build AST typedef struct RNode { VALUE flags; union {

    struct RNode *node; ... } u1; union { struct RNode *node; ... } u2; union { struct RNode *node; ... } u3; rb_code_location_t nd_loc; } NODE; Contain node_type Contain various data
  45. Build AST typedef struct RNode { VALUE flags; union {

    struct RNode *node; ... } u1; union { struct RNode *node; ... } u2; union { struct RNode *node; ... } u3; rb_code_location_t nd_loc; } NODE; Contain node_type Contain various data Contain Location information
  46. Build AST • Builds AST in actions. • $1 stands

    for the value of the 1st component (`arg`). arg | arg '+' arg { $$ = call_bin_op(p, $1, '+', $3, &@2, &@$); } static NODE * call_bin_op(struct parser_params *p, NODE *recv, ID id, NODE *arg1, const YYLTYPE *op_loc, const YYLTYPE *loc) { NODE *expr; value_expr(recv); value_expr(arg1); expr = NEW_OPCALL(recv, id, NEW_LIST(arg1, &arg1->nd_loc), loc); nd_set_line(expr, op_loc->beg_pos.lineno); return expr; }
  47. Build AST arg | arg '+' arg { $$ =

    call_bin_op(p, $1, '+', $3, &@2, &@$); } static NODE * call_bin_op(struct parser_params *p, NODE *recv, ID id, NODE *arg1, const YYLTYPE *op_loc, const YYLTYPE *loc) { NODE *expr; value_expr(recv); value_expr(arg1); expr = NEW_OPCALL(recv, id, NEW_LIST(arg1, &arg1->nd_loc), loc); nd_set_line(expr, op_loc->beg_pos.lineno); return expr; } Action
  48. Build AST arg | arg '+' arg { $$ =

    call_bin_op(p, $1, '+', $3, &@2, &@$); } static NODE * call_bin_op(struct parser_params *p, NODE *recv, ID id, NODE *arg1, const YYLTYPE *op_loc, const YYLTYPE *loc) { NODE *expr; value_expr(recv); value_expr(arg1); expr = NEW_OPCALL(recv, id, NEW_LIST(arg1, &arg1->nd_loc), loc); nd_set_line(expr, op_loc->beg_pos.lineno); return expr; }
  49. Build AST arg | arg '+' arg { $$ =

    call_bin_op(p, $1, '+', $3, &@2, &@$); } static NODE * call_bin_op(struct parser_params *p, NODE *recv, ID id, NODE *arg1, const YYLTYPE *op_loc, const YYLTYPE *loc) { NODE *expr; value_expr(recv); value_expr(arg1); expr = NEW_OPCALL(recv, id, NEW_LIST(arg1, &arg1->nd_loc), loc); nd_set_line(expr, op_loc->beg_pos.lineno); return expr; }
  50. Build AST arg | arg '+' arg { $$ =

    call_bin_op(p, $1, '+', $3, &@2, &@$); } static NODE * call_bin_op(struct parser_params *p, NODE *recv, ID id, NODE *arg1, const YYLTYPE *op_loc, const YYLTYPE *loc) { NODE *expr; value_expr(recv); value_expr(arg1); expr = NEW_OPCALL(recv, id, NEW_LIST(arg1, &arg1->nd_loc), loc); nd_set_line(expr, op_loc->beg_pos.lineno); return expr; } Create NODE_OPCALL
  51. Build AST $ ruby --dump=p -e '1 + 2' NODE_SCOPE

    NODE_PRELUDE NODE_OPCALL (:+) NODE_LIT (1) NODE_ARRAY NODE_LIT (2)
  52. Compile Parsing ___ \ Ruby script -> Tokens -> AST

    -> Byte code (insns / ISeq) __/ __/ Tokenization Compile
  53. Compile • Do compile. • See “compile.c”. $ ruby --dump=i

    -e '1 + 2' == disasm: #<ISeq:<main>@-e:1 (1,0)- (1,5)>============================== 0000 putobject_OP_INT2FIX_O_1_C_ ( 1)[Li] 0001 putobject 2 0003 opt_plus <callinfo!mid:+, argc:1, ARGS_SIMPLE>, <callcache> 0006 leave
  54. Compile • Do compile. • See “compile.c”. $ ruby --dump=i

    -e '1 + 2' == disasm: #<ISeq:<main>@-e:1 (1,0)- (1,5)>============================== 0000 putobject_OP_INT2FIX_O_1_C_ ( 1)[Li] 0001 putobject 2 0003 opt_plus <callinfo!mid:+, argc:1, ARGS_SIMPLE>, <callcache> 0006 leave ISeq 4 insn(s)
  55. References • "Ruby Hacking Guide" (Part 2: Syntax analysis) •

    https://ruby-hacking-guide.github.io/ [EN] • http://i.loveruby.net/ja/rhg/book/ [JA] • "Ruby Under a Microscope" / “Rubyͷ͘͠Έ"
  56. Goal • Branch coverage • To pass code locations to

    compile phase. • Method coverage • To store code locations on ISeq. • What should we implement • Embed code locations into each NODE.
  57. Hint • Original source of location information is Ruby script.

    • If we want to use location information in "n"th step, we should implement location information in "n-1"th step. • In this case, it's need to pass location information from "Tokenization" to "Compile" to use location information in compile phase. Parsing ___ \ Ruby script -> Tokens -> AST -> Byte code (insns / ISeq) __/ __/ Tokenization Compile
  58. parser_params crash course • One on the main data structure

    of parser. • Too Big!!! struct parser_params { rb_imemo_tmpbuf_t *heap; YYSTYPE *lval; struct { rb_strterm_t *strterm; VALUE (*gets)(struct parser_params*,VALUE); VALUE input; VALUE prevline; VALUE lastline; VALUE nextline; const char *pbeg; const char *pcur; const char *pend; const char *ptok; long gets_ptr; enum lex_state_e state; /* track the nest level of any parens "()[]{}" */ int paren_nest; /* keep p->lex.paren_nest at the beginning of lambda "->" to detect tLAMBEG and keyword_do_LAMBDA */ int lpar_beg; /* track the nest level of only braces "{}" */ int brace_nest; } lex; stack_type cond_stack; stack_type cmdarg_stack; int tokidx; int toksiz; int tokline; int heredoc_end; int heredoc_indent; int heredoc_line_indent; char *tokenbuf; struct local_vars *lvtbl; int line_count; int ruby_sourceline; /* current line no. */ char *ruby_sourcefile; /* current source file */ VALUE ruby_sourcefile_string; rb_encoding *enc; token_info *token_info; VALUE compile_option; VALUE debug_buffer; VALUE debug_output; ID cur_arg; rb_ast_t *ast; unsigned int command_start:1; unsigned int eofp: 1; unsigned int ruby__end__seen: 1; unsigned int debug: 1; unsigned int has_shebang: 1; unsigned int in_defined: 1; unsigned int in_main: 1; unsigned int in_kwarg: 1; unsigned int in_def: 1; unsigned int in_class: 1; unsigned int token_seen: 1; unsigned int token_info_enabled: 1; # if WARN_PAST_SCOPE unsigned int past_scope_enabled: 1; # endif unsigned int error_p: 1; unsigned int cr_seen: 1; #ifndef RIPPER /* Ruby core only */ unsigned int do_print: 1; unsigned int do_loop: 1; unsigned int do_chomp: 1; unsigned int do_split: 1; unsigned int warn_location: 1; NODE *eval_tree_begin; NODE *eval_tree; VALUE error_buffer; VALUE debug_lines; VALUE coverage; const struct rb_block *base_block; #else /* Ripper only */ VALUE delayed; int delayed_line; int delayed_col; VALUE value; VALUE result; VALUE parsing_thread; #endif };
  59. parser_params crash course • It has struct for lexer (`lex`).

    • Lexer processes input in units of lines. • *Basically* processes from top to bottom. struct parser_params { ... struct { ... VALUE prevline; VALUE lastline; VALUE nextline; const char *pbeg; const char *pcur; const char *pend; const char *ptok; ... } lex; ... }; Lines W W Pointers
  60. What is column /* parse.y */ /* Structure of Lexer

    Buffer: lex.pbeg lex.ptok lex.pcur lex.pend | | | | |------------+------------+------------| |<---------->| token */
  61. What is column • When token '+' is recognized (Left).

    • When token tINTEGER (2) is recognized (Right). 1 + 2 ^ ^^ ^ | || +--- lex.pend | |+----- lex.pcur | +------ lex.ptok +-------- lex.pbeg 1 + 2 ^ ^ ^ | | +--- lex.pcur, lex.pend | +----- lex.ptok +-------- lex.pbeg
  62. What is column • `lex.ptok - lex.pbeg` (begin) and `lex.pcur

    - lex.pbeg` (end). • Column is a difference between pointers when a token is recognized. |--| lex.pcur - lex.pbeg (end) |-| lex.ptok - lex.pbeg (begin) 1 + 2 ^ ^^ ^ | || +--- lex.pend | |+----- lex.pcur | +------ lex.ptok +-------- lex.pbeg
  63. What is column • We must store columns somewhere before

    next token is recognized. 1 + 2 ^ ^^ ^ | || +--- lex.pend | |+----- lex.pcur | +------ lex.ptok +-------- lex.pbeg 1 + 2 ^ ^ ^ | | +--- lex.pcur, lex.pend | +----- lex.ptok +-------- lex.pbeg
  64. From Ruby script to tokens • Copy location information to

    `YYLTYPE *yylloc` in `yylex`. • The `yylloc` argument is newly added to `yylex`. • Call `RUBY_SET_YYLLOC` to set `yylloc`.
  65. static enum yytokentype yylex(YYSTYPE *lval, YYLTYPE *yylloc, struct parser_params *p)

    { enum yytokentype t; p->lval = lval; lval->val = Qundef; t = parser_yylex(p); if (has_delayed_token(p)) dispatch_delayed_token(p, t); else if (t != 0) dispatch_scan_event(p, t); if (p->lex.strterm && (p->lex.strterm->flags & STRTERM_HEREDOC)) RUBY_SET_YYLLOC_FROM_STRTERM_HEREDOC(*yylloc); else RUBY_SET_YYLLOC(*yylloc); return t; }
  66. static enum yytokentype yylex(YYSTYPE *lval, YYLTYPE *yylloc, struct parser_params *p)

    { enum yytokentype t; p->lval = lval; lval->val = Qundef; t = parser_yylex(p); if (has_delayed_token(p)) dispatch_delayed_token(p, t); else if (t != 0) dispatch_scan_event(p, t); if (p->lex.strterm && (p->lex.strterm->flags & STRTERM_HEREDOC)) RUBY_SET_YYLLOC_FROM_STRTERM_HEREDOC(*yylloc); else RUBY_SET_YYLLOC(*yylloc); return t; } New argument
  67. static enum yytokentype yylex(YYSTYPE *lval, YYLTYPE *yylloc, struct parser_params *p)

    { enum yytokentype t; p->lval = lval; lval->val = Qundef; t = parser_yylex(p); if (has_delayed_token(p)) dispatch_delayed_token(p, t); else if (t != 0) dispatch_scan_event(p, t); if (p->lex.strterm && (p->lex.strterm->flags & STRTERM_HEREDOC)) RUBY_SET_YYLLOC_FROM_STRTERM_HEREDOC(*yylloc); else RUBY_SET_YYLLOC(*yylloc); return t; } New argument static enum yytokentype yylex(YYSTYPE *lval, YYLTYPE *yylloc, struct parser_params *p) { enum yytokentype t; p->lval = lval; lval->val = Qundef; t = parser_yylex(p); if (has_delayed_token(p)) dispatch_delayed_token(p, t); else if (t != 0) dispatch_scan_event(p, t); if (p->lex.strterm && (p->lex.strterm->flags & STRTERM_HEREDOC)) RUBY_SET_YYLLOC_FROM_STRTERM_HEREDOC(*yylloc); else RUBY_SET_YYLLOC(*yylloc); return t; } Create token
  68. static enum yytokentype yylex(YYSTYPE *lval, YYLTYPE *yylloc, struct parser_params *p)

    { enum yytokentype t; p->lval = lval; lval->val = Qundef; t = parser_yylex(p); if (has_delayed_token(p)) dispatch_delayed_token(p, t); else if (t != 0) dispatch_scan_event(p, t); if (p->lex.strterm && (p->lex.strterm->flags & STRTERM_HEREDOC)) RUBY_SET_YYLLOC_FROM_STRTERM_HEREDOC(*yylloc); else RUBY_SET_YYLLOC(*yylloc); return t; } New argument static enum yytokentype yylex(YYSTYPE *lval, YYLTYPE *yylloc, struct parser_params *p) { enum yytokentype t; p->lval = lval; lval->val = Qundef; t = parser_yylex(p); if (has_delayed_token(p)) dispatch_delayed_token(p, t); else if (t != 0) dispatch_scan_event(p, t); if (p->lex.strterm && (p->lex.strterm->flags & STRTERM_HEREDOC)) RUBY_SET_YYLLOC_FROM_STRTERM_HEREDOC(*yylloc); else RUBY_SET_YYLLOC(*yylloc); return t; } Create token Set `yylloc`
  69. From tokens to Nodes • Now we can use `@n`

    in each action. • `@n` stands for the location of the nth component of the right hand side. • `@$` stands for the location of the left hand side grouping (`YYLTYPE yyloc`). • Set by `YYLLOC_DEFAULT`. • https://www.gnu.org/software/bison/manual/html_node/ Tracking-Locations.html#Tracking-Locations
  70. |---| @$ (1.0-1.5) 1 + 2 ^ ^ ^ |

    | +- @3 (1.4-1.5) | +--- @2 (1.2-1.3) +----- @1 (1.0-1.1) arg | arg '+' arg { $$ = call_bin_op(p, $1, '+', $3, &@2, &@$); } static NODE * call_bin_op(struct parser_params *p, NODE *recv, ID id, NODE *arg1, const YYLTYPE *op_loc, const YYLTYPE *loc) { ... expr = NEW_OPCALL(recv, id, NEW_LIST(arg1, &arg1->nd_loc), loc); ... } arg | arg '+' arg { $$ = call_bin_op(p, $1, '+', $3, &@2, &@$); } static NODE * call_bin_op(struct parser_params *p, NODE *recv, ID id, NODE *arg1, const YYLTYPE *op_loc, const YYLTYPE *loc) { ... expr = NEW_OPCALL(recv, id, NEW_LIST(arg1, &arg1->nd_loc), loc); ... } 1 2
  71. |---| @$ (1.0-1.5) 1 + 2 ^ ^ ^ |

    | +- @3 (1.4-1.5) | +--- @2 (1.2-1.3) +----- @1 (1.0-1.1) arg | arg '+' arg { $$ = call_bin_op(p, $1, '+', $3, &@2, &@$); } static NODE * call_bin_op(struct parser_params *p, NODE *recv, ID id, NODE *arg1, const YYLTYPE *op_loc, const YYLTYPE *loc) { ... expr = NEW_OPCALL(recv, id, NEW_LIST(arg1, &arg1->nd_loc), loc); ... } (1.0-1.5) 1 2
  72. |---| @$ (1.0-1.5) 1 + 2 ^ ^ ^ |

    | +- @3 (1.4-1.5) | +--- @2 (1.2-1.3) +----- @1 (1.0-1.1) arg | arg '+' arg { $$ = call_bin_op(p, $1, '+', $3, &@2, &@$); } static NODE * call_bin_op(struct parser_params *p, NODE *recv, ID id, NODE *arg1, const YYLTYPE *op_loc, const YYLTYPE *loc) { ... expr = NEW_OPCALL(recv, id, NEW_LIST(arg1, &arg1->nd_loc), loc); ... } (1.0-1.5) arg | arg '+' arg { $$ = call_bin_op(p, $1, '+', $3, &@2, &@$); } static NODE * call_bin_op(struct parser_params *p, NODE *recv, ID id, NODE *arg1, const YYLTYPE *op_loc, const YYLTYPE *loc) { ... expr = NEW_OPCALL(recv, id, NEW_LIST(arg1, &arg1->nd_loc), loc); ... } 1 2
  73. |---| @$ (1.0-1.5) 1 + 2 ^ ^ ^ |

    | +- @3 (1.4-1.5) | +--- @2 (1.2-1.3) +----- @1 (1.0-1.1) arg | arg '+' arg { $$ = call_bin_op(p, $1, '+', $3, &@2, &@$); } static NODE * call_bin_op(struct parser_params *p, NODE *recv, ID id, NODE *arg1, const YYLTYPE *op_loc, const YYLTYPE *loc) { ... expr = NEW_OPCALL(recv, id, NEW_LIST(arg1, &arg1->nd_loc), loc); ... } (1.0-1.5) arg | arg '+' arg { $$ = call_bin_op(p, $1, '+', $3, &@2, &@$); } static NODE * call_bin_op(struct parser_params *p, NODE *recv, ID id, NODE *arg1, const YYLTYPE *op_loc, const YYLTYPE *loc) { ... expr = NEW_OPCALL(recv, id, NEW_LIST(arg1, &arg1->nd_loc), loc); ... } 1 2
  74. `NODE_OPCALL` is simple • All location needed to NODE_OPCALL is

    supplied when create NODE_OPCALL. • NODE_LIT (1) • mid (:+) • NODE_ARRAY (2) NODE_OPCALL 1 + 2 ----------- ------------ arg : arg '+' arg
  75. `NODE_*ASGN` family is not simple • `NODE_LASGN` (local variable assignment)

    or `NODE_IASGN` (instance variable assignment). @a = 1 NODE_SCOPE NODE_IASGN (:@a) NODE_LIT (1)
  76. NODE_IASGN @a ---------- ------------- lhs : user_variable { /*%%%*/ $$

    = assignable(p, $1, 0, &@$); /*% %*/ } NODE_IASGN NODE_IASGN = 1 ---------- ------------------------ arg : lhs '=' arg_rhs { /*%%%*/ $$ = node_assign(p, $1, $3, &@$); /*% %*/ /*% ripper: assign!($1, $3) %*/ }
  77. NODE_IASGN @a ---------- ------------- lhs : user_variable { /*%%%*/ $$

    = assignable(p, $1, 0, &@$); /*% %*/ } NODE_IASGN NODE_IASGN = 1 ---------- ------------------------ arg : lhs '=' arg_rhs { /*%%%*/ $$ = node_assign(p, $1, $3, &@$); /*% %*/ /*% ripper: assign!($1, $3) %*/ } Create NODE_IASGN
  78. NODE_IASGN @a ---------- ------------- lhs : user_variable { /*%%%*/ $$

    = assignable(p, $1, 0, &@$); /*% %*/ } NODE_IASGN NODE_IASGN = 1 ---------- ------------------------ arg : lhs '=' arg_rhs { /*%%%*/ $$ = node_assign(p, $1, $3, &@$); /*% %*/ /*% ripper: assign!($1, $3) %*/ } Create NODE_IASGN Location is determined
  79. NODE_IASGN @a ---------- ------------- lhs : user_variable { /*%%%*/ $$

    = assignable(p, $1, 0, &@$); /*% %*/ } NODE_IASGN NODE_IASGN = 1 ---------- ------------------------ arg : lhs '=' arg_rhs { /*%%%*/ $$ = node_assign(p, $1, $3, &@$); /*% %*/ /*% ripper: assign!($1, $3) %*/ } Create NODE_IASGN Update location Location is determined
  80. `NODE_*ASGN` family is not simple • Create `NODE_*ASGN`. • Assign

    right hand side. • So it's needed to update location of `NODE_*ASGN` when right hand side is assigned.
  81. `NODE_ITER` is not simple • `NODE_ITER` (method call with block).

    3.times { foo } NODE_ITER NODE_CALL (:times) NODE_LIT (3) NODE_SCOPE NODE_VCALL (:foo)
  82. NODE_CALL 3 . times ----------- -------------------------------------------------- method_call | primary_value call_op

    operation2 opt_paren_args { } NODE_ITER { NODE_ITER } ----------- --------------------- brace_block : '{' brace_body '}' { } NODE_ITER NODE_CALL NODE_ITER --------- ----------------------- primary | method_call brace_block { /*%%%*/ block_dup_check(p, $1->nd_args, $2); $$ = method_add_block(p, $1, $2, &@$); /*% %*/ }
  83. NODE_CALL 3 . times ----------- -------------------------------------------------- method_call | primary_value call_op

    operation2 opt_paren_args { } NODE_ITER { NODE_ITER } ----------- --------------------- brace_block : '{' brace_body '}' { } NODE_ITER NODE_CALL NODE_ITER --------- ----------------------- primary | method_call brace_block { /*%%%*/ block_dup_check(p, $1->nd_args, $2); $$ = method_add_block(p, $1, $2, &@$); /*% %*/ } `3.times` `{ foo }`
  84. NODE_CALL 3 . times ----------- -------------------------------------------------- method_call | primary_value call_op

    operation2 opt_paren_args { } NODE_ITER { NODE_ITER } ----------- --------------------- brace_block : '{' brace_body '}' { } NODE_ITER NODE_CALL NODE_ITER --------- ----------------------- primary | method_call brace_block { /*%%%*/ block_dup_check(p, $1->nd_args, $2); $$ = method_add_block(p, $1, $2, &@$); /*% %*/ } `3.times` `{ foo }` Update location
  85. `NODE_ITER` is not simple • `NODE_CALL` is created. • `NODE_ITER`

    is created. • `NODE_ITER` is added to `NODE_CALL`. • It's needed to update location of `NODE_ITER` when it is passed to `NODE_CALL`.
  86. $ git shortlog -s -n parse.y | head -10 XXX

    ???? 362 matz 133 mame 88 yui-knk 55 ko1 38 aamine 37 akr 33 naruse 25 usa 7 normal On "v2_6_0_preview1".
  87. $ git shortlog -s -n parse.y | head -10 XXX

    ???? 362 matz 133 mame 88 yui-knk 55 ko1 38 aamine 37 akr 33 naruse 25 usa 7 normal On "v2_6_0_preview1". Me
  88. $ git shortlog -s -n parse.y | head -10 884

    nobu 362 matz 133 mame 88 yui-knk 55 ko1 38 aamine 37 akr 33 naruse 25 usa 7 normal On "v2_6_0_preview1".
  89. $ git shortlog -s -n parse.y | head -10 884

    nobu 362 matz 133 mame 88 yui-knk 55 ko1 38 aamine 37 akr 33 naruse 25 usa 7 normal On "v2_6_0_preview1". x 10
  90. $ git shortlog -s -n parse.y | head -10 884

    nobu 362 matz 133 mame 88 yui-knk 55 ko1 38 aamine 37 akr 33 naruse 25 usa 7 normal On "v2_6_0_preview1". x 10 @nobu is the lord of Demon Castle "parse.y".
  91. How to test • Define some rules and check all

    ruby files in "test" directory follow the rules. • Related files: • “ext/-test-/ast/ast.c" • "test/-ext-/ast/test_ast.rb" On "v2_6_0_preview1".
  92. "ext/-test-" and "test/-ext-" • "ext/-test-" contains C extensions which are

    used in Ruby's tests. • "ext/-test-/ast/ast.c" defines `AST` module and `AST::Node` class. On "v2_6_0_preview1".
  93. Rule 1 • `lineno` is initialized with `0` and `column`

    with `-1`. • Validate all node locations are update at least once. NODE_IF (line: 1, location: (0,-1)-(0,-1))
  94. Rule 1 • `lineno` is initialized with `0` and `column`

    with `-1`. • Validate all node locations are update at least once. NODE_IF (line: 1, location: (0,-1)-(0,-1))
  95. Rule 2 • Validate children do not exceed a parent

    location. 3.times { foo } NODE_ITER [1.0-1.15] NODE_CALL (:times) [1.0-1.8] NODE_LIT (3) [1.0-1.1] NODE_SCOPE [1.8-1.15] NODE_VCALL (:foo) [1.10-1.13]
  96. Rule 2 • Validate children do not exceed a parent

    location. 3.times { foo } NODE_ITER [1.0-1.15] NODE_CALL (:times) [1.0-1.8] NODE_LIT (3) [1.0-1.1] NODE_SCOPE [1.8-1.15] -> covers [1.10-1.13] NODE_VCALL (:foo) [1.10-1.13]
  97. Rule 2 • Validate children do not exceed a parent

    location. 3.times { foo } NODE_ITER [1.0-1.15] NODE_CALL (:times) [1.0-1.8] -> covers [1.0-1.1] NODE_LIT (3) [1.0-1.1] NODE_SCOPE [1.8-1.15] -> covers [1.10-1.13] NODE_VCALL (:foo) [1.10-1.13]
  98. Rule 2 • Validate children do not exceed a parent

    location. 3.times { foo } NODE_ITER [1.0-1.15] -> covers [1.0-1.8] and [1.8-1.15] NODE_CALL (:times) [1.0-1.8] -> covers [1.0-1.1] NODE_LIT (3) [1.0-1.1] NODE_SCOPE [1.8-1.15] -> covers [1.10-1.13] NODE_VCALL (:foo) [1.10-1.13]
  99. Rule 2 • Validate children do not exceed a parent

    location. 3.times { foo } NODE_ITER [1.0-1.15] -> covers [1.0-1.8] and [1.8-1.15] NODE_CALL (:times) [1.0-1.8] -> covers [1.0-1.1] NODE_LIT (3) [1.0-1.1] NODE_SCOPE [1.8-1.15] -> covers [1.10-1.13] NODE_VCALL (:foo) [1.10-1.13]
  100. Dir.glob("test/**/*.rb", base: SRCDIR).each do |path| define_method("test_ranges:#{path}") do helper = Helper.new("#{SRCDIR}/#{path}")

    helper.validate_range assert_equal([], helper.errors) end end test/-ext-/ast/test_ast.rb
  101. Dir.glob("test/**/*.rb", base: SRCDIR).each do |path| define_method("test_ranges:#{path}") do helper = Helper.new("#{SRCDIR}/#{path}")

    helper.validate_range assert_equal([], helper.errors) end end Check all ruby files in "test" directory test/-ext-/ast/test_ast.rb
  102. Dir.glob("test/**/*.rb", base: SRCDIR).each do |path| define_method("test_ranges:#{path}") do helper = Helper.new("#{SRCDIR}/#{path}")

    helper.validate_range assert_equal([], helper.errors) end end Check all ruby files in "test" directory Validate each file test/-ext-/ast/test_ast.rb
  103. def validate_range0(node) beg_pos, end_pos = node.beg_pos, node.end_pos children = node.children.compact

    min = children.map(&:beg_pos).min max = children.map(&:end_pos).max unless beg_pos <= min @errors << { type: :min_validation_error, min: min, beg_pos: beg_pos, node: node } end unless max <= end_pos @errors << { type: :max_validation_error, max: max, end_pos: end_pos, node: node } end children.each do |child| validate_range0(child) end end ast = AST.parse_file(@path) validate_not_cared0(ast) test/-ext-/ast/test_ast.rb
  104. def validate_range0(node) beg_pos, end_pos = node.beg_pos, node.end_pos children = node.children.compact

    min = children.map(&:beg_pos).min max = children.map(&:end_pos).max unless beg_pos <= min @errors << { type: :min_validation_error, min: min, beg_pos: beg_pos, node: node } end unless max <= end_pos @errors << { type: :max_validation_error, max: max, end_pos: end_pos, node: node } end children.each do |child| validate_range0(child) end end ast = AST.parse_file(@path) validate_not_cared0(ast) Generate AST test/-ext-/ast/test_ast.rb
  105. def validate_range0(node) beg_pos, end_pos = node.beg_pos, node.end_pos children = node.children.compact

    min = children.map(&:beg_pos).min max = children.map(&:end_pos).max unless beg_pos <= min @errors << { type: :min_validation_error, min: min, beg_pos: beg_pos, node: node } end unless max <= end_pos @errors << { type: :max_validation_error, max: max, end_pos: end_pos, node: node } end children.each do |child| validate_range0(child) end end ast = AST.parse_file(@path) validate_not_cared0(ast) Generate AST test/-ext-/ast/test_ast.rb Check ranges
  106. def validate_range0(node) beg_pos, end_pos = node.beg_pos, node.end_pos children = node.children.compact

    min = children.map(&:beg_pos).min max = children.map(&:end_pos).max unless beg_pos <= min @errors << { type: :min_validation_error, min: min, beg_pos: beg_pos, node: node } end unless max <= end_pos @errors << { type: :max_validation_error, max: max, end_pos: end_pos, node: node } end children.each do |child| validate_range0(child) end end ast = AST.parse_file(@path) validate_not_cared0(ast) Generate AST test/-ext-/ast/test_ast.rb Check ranges Check children
  107. Case 1 (Proc/Method) • Add new methods to Proc/Method which

    return their code location. def a(&block) p block.code_location end a do 1 + 2 end # => [[5, 2], [7, 3]] p self.class.instance_method(:a).code_location # => [[1, 0], [3, 3]] https://github.com/yui-knk/ruby/tree/feature/rb_iseq_code_location
  108. Case 1 (Proc/Method) • Add new methods to Proc/Method which

    return their code location. def a(&block) p block.code_location end a do 1 + 2 end # => [[5, 2], [7, 3]] p self.class.instance_method(:a).code_location # => [[1, 0], [3, 3]] https://github.com/yui-knk/ruby/tree/feature/rb_iseq_code_location
  109. Case 1 (Proc/Method) • Add new methods to Proc/Method which

    return their code location. def a(&block) p block.code_location end a do 1 + 2 end # => [[5, 2], [7, 3]] p self.class.instance_method(:a).code_location # => [[1, 0], [3, 3]] https://github.com/yui-knk/ruby/tree/feature/rb_iseq_code_location
  110. Case 1 (Proc/Method) • Add new methods to Proc/Method which

    return their code location. def a(&block) p block.code_location end a do 1 + 2 end # => [[5, 2], [7, 3]] p self.class.instance_method(:a).code_location # => [[1, 0], [3, 3]] https://github.com/yui-knk/ruby/tree/feature/rb_iseq_code_location
  111. Case 2 (NoMethodError) • Give `NoMethodError` more detailed message. class

    A def foo nil end end A.new.foo.foo Traceback (most recent call last): /tmp/test.rb:7:in `<main>': undefined method `foo' for nil:NilClass (NoMethodError) A.new.foo.foo ^^^^ https://github.com/yui-knk/ruby/tree/feature/node_id
  112. Case 3 (AST module) AST.parse("1 + 2") # => #<AST::Node(NODE_SCOPE(0)

    1:0, 1:5 (4)): > AST.parse("1 + 2").children[1] # => #<AST::Node(NODE_OPCALL(36) 1:0, 1:5 (3)): > AST.parse("1 + 2").children[1].children # => [#<AST::Node(NODE_LIT(59) 1:0, 1:1 (0)): >, #<AST::Node(NODE_ARRAY(42) 1:4, 1:5 (2)): >]
  113. Case 3 (AST module) AST.parse("1 + 2") # => #<AST::Node(NODE_SCOPE(0)

    1:0, 1:5 (4)): > AST.parse("1 + 2").children[1] # => #<AST::Node(NODE_OPCALL(36) 1:0, 1:5 (3)): > AST.parse("1 + 2").children[1].children # => [#<AST::Node(NODE_LIT(59) 1:0, 1:1 (0)): >, #<AST::Node(NODE_ARRAY(42) 1:4, 1:5 (2)): >]
  114. Case 3 (AST module) • We can get children nodes.

    RubyVM::AST.parse("1 + 2") # => #<RubyVM::AST::Node(NODE_SCOPE(0) 1:0, 1:5): > RubyVM::AST.parse("1 + 2").children[1] # => #<RubyVM::AST::Node(NODE_OPCALL(36) 1:0, 1:5): > RubyVM::AST.parse("1 + 2").children[1].children # => [#<RubyVM::AST::Node(NODE_LIT(59) 1:0, 1:1): >, #<RubyVM::AST::Node(NODE_ARRAY(42) 1:4, 1:5): >]
  115. Case 3 (AST module) • We can get location information.

    [RubyVM::AST.parse("1 + 2").first_lineno, RubyVM::AST.parse("1 + 2").first_column] # => [1, 0] [RubyVM::AST.parse("1 + 2").last_lineno, RubyVM::AST.parse("1 + 2").last_column] # => [1, 5]
  116. Acknowledgments • @mametter • @nobu • @ko1 • @shyouhei •

    @takeshinoda • @hkdnet • @HaiTo • @littlestarling
  117. Conclusion • AST Node has location information. • Share the

    future plan of code locations feature. • If you have any idea to use location information, please let me know :) • https://bugs.ruby-lang.org/ • You now get the map of Demon Castle "parse.y", let's hack “parse.y" :)
  118. # @ NODE_CALL (line: 7, location: (7,0)-(7,13))* 13 # +-

    nd_mid: :foo # +- nd_recv: # | @ NODE_CALL (line: 7, location: (7,0)-(7,9)) 12 # | +- nd_mid: :foo # | +- nd_recv: # | | @ NODE_CALL (line: 7, location: (7,0)-(7,5)) 11 # | | +- nd_mid: :new # | | +- nd_recv: # | | | @ NODE_CONST (line: 7, location: (7,0)-(7,1)) 10 # | | | +- nd_vid: :A # | | +- nd_args: # | | (null node) # | +- nd_args: # | (null node) # +- nd_args: # (null node) node_id • Add unique id (per file), “node_id”, to Node.
  119. == disasm: #<ISeq:<main>@src/no_method_error2.rb:1 (1,0)-(7,13)> (catch: FALSE) 0000 putspecialobject 3 (

    1)[ 0][Li] 0002 putnil [ 9] 0003 defineclass :A, <class:A>, 0 0007 pop 0008 getinlinecache 15, <is:0> ( 7)[ 10][Li] 0011 getconstant :A 0013 setinlinecache <is:0> 0015 opt_send_without_block <callinfo!mid:new, argc:0, ARGS_SIMPLE>, <callcache>[ 11] 0018 opt_send_without_block <callinfo!mid:foo, argc:0, ARGS_SIMPLE>, <callcache>[ 12] 0021 opt_send_without_block <callinfo!mid:foo, argc:0, ARGS_SIMPLE>, <callcache>[ 13] 0024 leave [ 10] node_id • Store node_id of insn on an ISeq.
  120. • Store node_id of insn on an ISeq. • We

    can distinguish between `#foo`s by node_id. == disasm: #<ISeq:<main>@src/no_method_error2.rb:1 (1,0)-(7,13)> (catch: FALSE) 0000 putspecialobject 3 ( 1)[ 0][Li] 0002 putnil [ 9] 0003 defineclass :A, <class:A>, 0 0007 pop 0008 getinlinecache 15, <is:0> ( 7)[ 10][Li] 0011 getconstant :A 0013 setinlinecache <is:0> 0015 opt_send_without_block <callinfo!mid:new, argc:0, ARGS_SIMPLE>, <callcache>[ 11] 0018 opt_send_without_block <callinfo!mid:foo, argc:0, ARGS_SIMPLE>, <callcache>[ 12] 0021 opt_send_without_block <callinfo!mid:foo, argc:0, ARGS_SIMPLE>, <callcache>[ 13] 0024 leave [ 10]
  121. Exceptions • Exceptions have an ISeq and a program counter

    (pc). == disasm: #<ISeq:<main>@src/no_method_error2.rb:1 (1,0)-(7,13)> (catch: FALSE) 0000 putspecialobject 3 ( 1)[ 0][Li] 0002 putnil [ 9] 0003 defineclass :A, <class:A>, 0 0007 pop 0008 getinlinecache 15, <is:0> ( 7)[ 10][Li] 0011 getconstant :A 0013 setinlinecache <is:0> 0015 opt_send_without_block <callinfo!mid:new, argc:0, ARGS_SIMPLE>, <callcache>[ 11] 0018 opt_send_without_block <callinfo!mid:foo, argc:0, ARGS_SIMPLE>, <callcache>[ 12] 0021 opt_send_without_block <callinfo!mid:foo, argc:0, ARGS_SIMPLE>, <callcache>[ 13] 0024 leave [ 10]
  122. Exceptions • Exceptions have an ISeq and a program counter

    (pc). == disasm: #<ISeq:<main>@src/no_method_error2.rb:1 (1,0)-(7,13)> (catch: FALSE) 0000 putspecialobject 3 ( 1)[ 0][Li] 0002 putnil [ 9] 0003 defineclass :A, <class:A>, 0 0007 pop 0008 getinlinecache 15, <is:0> ( 7)[ 10][Li] 0011 getconstant :A 0013 setinlinecache <is:0> 0015 opt_send_without_block <callinfo!mid:new, argc:0, ARGS_SIMPLE>, <callcache>[ 11] 0018 opt_send_without_block <callinfo!mid:foo, argc:0, ARGS_SIMPLE>, <callcache>[ 12] 0021 opt_send_without_block <callinfo!mid:foo, argc:0, ARGS_SIMPLE>, <callcache>[ 13] 0024 leave [ 10] Exception
  123. Exceptions • Exceptions have an ISeq and a program counter

    (pc). == disasm: #<ISeq:<main>@src/no_method_error2.rb:1 (1,0)-(7,13)> (catch: FALSE) 0000 putspecialobject 3 ( 1)[ 0][Li] 0002 putnil [ 9] 0003 defineclass :A, <class:A>, 0 0007 pop 0008 getinlinecache 15, <is:0> ( 7)[ 10][Li] 0011 getconstant :A 0013 setinlinecache <is:0> 0015 opt_send_without_block <callinfo!mid:new, argc:0, ARGS_SIMPLE>, <callcache>[ 11] 0018 opt_send_without_block <callinfo!mid:foo, argc:0, ARGS_SIMPLE>, <callcache>[ 12] 0021 opt_send_without_block <callinfo!mid:foo, argc:0, ARGS_SIMPLE>, <callcache>[ 13] 0024 leave [ 10] Exception
  124. Exceptions • Get node_id from an exception. == disasm: #<ISeq:<main>@src/no_method_error2.rb:1

    (1,0)-(7,13)> (catch: FALSE) 0000 putspecialobject 3 ( 1)[ 0][Li] 0002 putnil [ 9] 0003 defineclass :A, <class:A>, 0 0007 pop 0008 getinlinecache 15, <is:0> ( 7)[ 10][Li] 0011 getconstant :A 0013 setinlinecache <is:0> 0015 opt_send_without_block <callinfo!mid:new, argc:0, ARGS_SIMPLE>, <callcache>[ 11] 0018 opt_send_without_block <callinfo!mid:foo, argc:0, ARGS_SIMPLE>, <callcache>[ 12] 0021 opt_send_without_block <callinfo!mid:foo, argc:0, ARGS_SIMPLE>, <callcache>[ 13] 0024 leave [ 10] Exception Get node_id (13)
  125. # @ NODE_CALL (line: 7, location: (7,0)-(7,13))* 13 # +-

    nd_mid: :foo # +- nd_recv: # | @ NODE_CALL (line: 7, location: (7,0)-(7,9)) 12 # | +- nd_mid: :foo # | +- nd_recv: # | | @ NODE_CALL (line: 7, location: (7,0)-(7,5)) 11 # | | +- nd_mid: :new # | | +- nd_recv: # | | | @ NODE_CONST (line: 7, location: (7,0)-(7,1)) 10 # | | | +- nd_vid: :A # | | +- nd_args: # | | (null node) # | +- nd_args: # | (null node) # +- nd_args: # (null node) • Parse the source code file and find Node by node_id.
  126. # @ NODE_CALL (line: 7, location: (7,0)-(7,13))* 13 # +-

    nd_mid: :foo # +- nd_recv: # | @ NODE_CALL (line: 7, location: (7,0)-(7,9)) 12 # | +- nd_mid: :foo # | +- nd_recv: # | | @ NODE_CALL (line: 7, location: (7,0)-(7,5)) 11 # | | +- nd_mid: :new # | | +- nd_recv: # | | | @ NODE_CONST (line: 7, location: (7,0)-(7,1)) 10 # | | | +- nd_vid: :A # | | +- nd_args: # | | (null node) # | +- nd_args: # | (null node) # +- nd_args: # (null node) • Parse the source code file and find Node by node_id.
  127. # @ NODE_CALL (line: 7, location: (7,0)-(7,13))* 13 # +-

    nd_mid: :foo # +- nd_recv: # | @ NODE_CALL (line: 7, location: (7,0)-(7,9)) 12 # | +- nd_mid: :foo # | +- nd_recv: # | | @ NODE_CALL (line: 7, location: (7,0)-(7,5)) 11 # | | +- nd_mid: :new # | | +- nd_recv: # | | | @ NODE_CONST (line: 7, location: (7,0)-(7,1)) 10 # | | | +- nd_vid: :A # | | +- nd_args: # | | (null node) # | +- nd_args: # | (null node) # +- nd_args: # (null node) • Parse the source code file and find Node by node_id.
  128. A.new.foo.foo NODE_CALL (:foo) (line: 7, location: (7,0)-(7,13))* 13 NODE_CALL (:foo)

    (line: 7, location: (7,0)-(7,9)) 12 NODE_CALL (:new) (line: 7, location: (7,0)-(7,5)) 11 NODE_CONST (:A) (line: 7, location: (7,0)-(7,1)) 10 • Get location of Node.
  129. A.new.foo.foo NODE_CALL (:foo) (line: 7, location: (7,0)-(7,13))* 13 NODE_CALL (:foo)

    (line: 7, location: (7,0)-(7,9)) 12 NODE_CALL (:new) (line: 7, location: (7,0)-(7,5)) 11 NODE_CONST (:A) (line: 7, location: (7,0)-(7,1)) 10 • Get location of Node.
  130. A.new.foo.foo NODE_CALL (:foo) (line: 7, location: (7,0)-(7,13))* 13 NODE_CALL (:foo)

    (line: 7, location: (7,0)-(7,9)) 12 NODE_CALL (:new) (line: 7, location: (7,0)-(7,5)) 11 NODE_CONST (:A) (line: 7, location: (7,0)-(7,1)) 10 A.new.foo • Get location of Node.
  131. A.new.foo.foo NODE_CALL (:foo) (line: 7, location: (7,0)-(7,13))* 13 NODE_CALL (:foo)

    (line: 7, location: (7,0)-(7,9)) 12 NODE_CALL (:new) (line: 7, location: (7,0)-(7,5)) 11 NODE_CONST (:A) (line: 7, location: (7,0)-(7,1)) 10 A.new.foo .foo • Build an error message.
  132. A.new.foo.foo NODE_CALL (:foo) (line: 7, location: (7,0)-(7,13))* 13 NODE_CALL (:foo)

    (line: 7, location: (7,0)-(7,9)) 12 NODE_CALL (:new) (line: 7, location: (7,0)-(7,5)) 11 NODE_CONST (:A) (line: 7, location: (7,0)-(7,1)) 10 A.new.foo .foo ^^^^ • Build an error message.
  133. How to implement more detailed message of `NoMethodError` • Add

    unique id (per file), “node_id”, to Node. • Store node_id of insn on an ISeq. • Get node_id from an exception. • Parse the source code file and find Node by node_id. • Get location of Node. • Build an error message.