Class: LazyPP::Parser

Inherits:
Parslet::Parser
  • Object
show all
Includes:
ParserMethods
Defined in:
lib/readable-cpp/parser.rb

Instance Method Summary collapse

Methods included from ParserMethods

#`, #comma_list, included, #join

Instance Method Details

#brackets(rule) ⇒ Object



5
6
7
# File 'lib/readable-cpp/parser.rb', line 5

def brackets(rule) 
  l_bracket >> spaced?(rule) >> r_bracket
end

#brackets?(rule) ⇒ Boolean

Returns:

  • (Boolean)


8
9
10
# File 'lib/readable-cpp/parser.rb', line 8

def brackets?(rule)
  brackets(rule) | rule
end

#derived_englishObject

UNFINISHED



145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
# File 'lib/readable-cpp/parser.rb', line 145

rule(:derived_english) {
  join(
    const.as(:const) >> (space >> (ref_eng|ptr_eng)).present? |
    ptr_eng.as(:ptr) |
    ref_eng.as(:ref) |
    ##Think I will go with this, 
    ## array of 5 .. or array of (CONSTEXPR) .. or array of ..
    str(?[)>> spaced?(expr.maybe.as(:array)) >> str(?]) |
    `array of` >> (
      (space >> int.as(:array) | space? >> parens(expr).as(:array)) |
      any.present?.as(:array_unsized)
    ) |
    (
      `function` >> `s`.maybe >> (space >> `taking`).maybe >> 
      (space? >> func_sig_args | space >> func_sig_args_innards) >>
      space? >> (str('->') | returnsing)
    ) |
    func_sig_args >> space? >> (str('->') | returnsing),

    space
  )
}

#func_sig_args_innardsObject

func sig without parens



488
489
490
491
# File 'lib/readable-cpp/parser.rb', line 488

rule(:func_sig_args_innards) { ##func sig without parens 
  ( func_sig_args_innards__   ##used with english `function taking ..`
  ).as(:func_sig)
}

#func_sig_nosaveObject

func_sig.present? didnt work, but this does..



458
459
460
461
462
463
# File 'lib/readable-cpp/parser.rb', line 458

rule(:func_sig_nosave) { ##func_sig.present? didnt work, but this does..
  (specifiers >> space?).maybe >> (storage >> space?).maybe >>
  func_sig_args >> 
  ##parens(`void` | any_arg | ident_defs | comma_list(type)) >>
  spaced?(str('->')) >> type
}

#infix_expr(left, op, right) ⇒ Object

16 is the entry point for expressions. 17 is available from inside parens



757
758
759
760
# File 'lib/readable-cpp/parser.rb', line 757

def infix_expr left, op, right
  ( left.as(:left) >> spaced?(op.as(:op)) >> right.as(:right)
  ).as(:infix)
end

#paren_tagged(rule, tag = :parens) ⇒ Object



27
28
29
# File 'lib/readable-cpp/parser.rb', line 27

def paren_tagged(rule, tag=:parens)
  parens(rule.as(tag))
end

#paren_tagged?(rule, tag) ⇒ Boolean

Returns:

  • (Boolean)


30
31
32
33
# File 'lib/readable-cpp/parser.rb', line 30

def paren_tagged?(rule, tag)
  paren_tagged(rule,tag) |
  rule
end

#parens(rule) ⇒ Object



21
22
23
# File 'lib/readable-cpp/parser.rb', line 21

def parens(rule)
  l_paren >> spaced?(rule) >> r_paren
end

#parens?(rule) ⇒ Boolean

Returns:

  • (Boolean)


24
25
26
# File 'lib/readable-cpp/parser.rb', line 24

def parens?(rule)
  parents(rule) | rule
end

#parens_oder_spacen(rule) ⇒ Object



17
18
19
20
# File 'lib/readable-cpp/parser.rb', line 17

def parens_oder_spacen(rule)
  parens(rule) |
  space >> rule
end

#space_commentsObject

TODO save comments in program



49
50
51
# File 'lib/readable-cpp/parser.rb', line 49

rule(:space_comments) {
  (comment.as(:comment) | match['\s'].repeat(1)).repeat(1)
}

#spaced(rule) ⇒ Object



11
12
13
# File 'lib/readable-cpp/parser.rb', line 11

def spaced(rule)
  space >> rule >> space
end

#spaced?(rule) ⇒ Boolean

Returns:

  • (Boolean)


14
15
16
# File 'lib/readable-cpp/parser.rb', line 14

def spaced?(rule)
  space? >> rule >> space?
end

#var_decl(terminator = semicolon_terminal) ⇒ Object



245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
# File 'lib/readable-cpp/parser.rb', line 245

def var_decl(terminator = semicolon_terminal)
#rule(:var_decl) {
  `var` >> space >> 
  join(
    ( var_decl_assign | var_decl_initialize
    ).as(:var_decl),

    spaced?(comma)
  ).as(:stmts) >> terminator
  # join(
  #   ( #decl/assign
  #     ident_no_keyword.as(:name) >> colon_is  >>
  #     type.as(:type) >> spaced?(str(?=)) >> expr.as(:expr) |
  #     #decl/initialize
  #     comma_list(ident_no_keyword.as(:name) >> 
  #       parens(comma_list(expr).maybe).maybe.as(:constructor)).as(:vdi_names) >>
  #     colon_is >> type.as(:vdi_type)
  #   ).as(:var_decl),

  #   spaced?(comma) ##I wanted to allow space here but it fails for
  #     ## "var x: short \n  foo, bar: int" #=> "short foo x; int bar;"
  #     ## would require the spacing in TYPE to be nonterminal
  # ).as(:stmts) >> terminator
#}
end