Class: Code::Parser::Function

Inherits:
Language
  • Object
show all
Defined in:
lib/code/parser/function.rb

Instance Method Summary collapse

Instance Method Details

#ampersandObject



86
87
88
# File 'lib/code/parser/function.rb', line 86

def ampersand
  str("&")
end

#asteriskObject



82
83
84
# File 'lib/code/parser/function.rb', line 82

def asterisk
  str("*")
end

#begin_keywordObject



38
39
40
# File 'lib/code/parser/function.rb', line 38

def begin_keyword
  str("begin") << separator.present
end

#bodyObject



116
117
118
119
120
121
122
123
124
125
# File 'lib/code/parser/function.rb', line 116

def body
  (
    (begin_keyword | do_keyword).ignore << whitespace << code <<
      (whitespace? << end_keyword).maybe.ignore
  ) |
    (
      opening_curly_bracket.ignore << whitespace? << code <<
        (whitespace? << closing_curly_bracket).maybe.ignore
    ) | (code << (whitespace? << end_keyword).maybe.ignore)
end

#closing_curly_bracketObject



78
79
80
# File 'lib/code/parser/function.rb', line 78

def closing_curly_bracket
  str("}")
end

#closing_parenthesisObject



54
55
56
# File 'lib/code/parser/function.rb', line 54

def closing_parenthesis
  str(")")
end

#codeObject



14
15
16
# File 'lib/code/parser/function.rb', line 14

def code
  Code
end

#code_presentObject



18
19
20
# File 'lib/code/parser/function.rb', line 18

def code_present
  Code.new.present
end

#colonObject



58
59
60
# File 'lib/code/parser/function.rb', line 58

def colon
  str(":")
end

#commaObject



62
63
64
# File 'lib/code/parser/function.rb', line 62

def comma
  str(",")
end

#do_keywordObject



34
35
36
# File 'lib/code/parser/function.rb', line 34

def do_keyword
  str("do") << separator.present
end

#end_keywordObject



42
43
44
# File 'lib/code/parser/function.rb', line 42

def end_keyword
  str("end") << separator.present
end

#equalObject



66
67
68
# File 'lib/code/parser/function.rb', line 66

def equal
  str("=")
end

#greaterObject



70
71
72
# File 'lib/code/parser/function.rb', line 70

def greater
  str(">")
end

#keyword_parameterObject



96
97
98
99
# File 'lib/code/parser/function.rb', line 96

def keyword_parameter
  label_name.aka(:name) << whitespace? << colon.aka(:keyword) <<
    code_present.aka(:default).maybe
end

#label_nameObject



10
11
12
# File 'lib/code/parser/function.rb', line 10

def label_name
  LabelName
end

#nameObject



6
7
8
# File 'lib/code/parser/function.rb', line 6

def name
  Name
end

#opening_curly_bracketObject



74
75
76
# File 'lib/code/parser/function.rb', line 74

def opening_curly_bracket
  str("{")
end

#opening_parenthesisObject



50
51
52
# File 'lib/code/parser/function.rb', line 50

def opening_parenthesis
  str("(")
end

#parameterObject



106
107
108
# File 'lib/code/parser/function.rb', line 106

def parameter
  keyword_parameter | regular_parameter
end

#parametersObject



110
111
112
113
114
# File 'lib/code/parser/function.rb', line 110

def parameters
  opening_parenthesis.ignore << whitespace? <<
    (whitespace? << parameter << (whitespace? << comma).maybe).repeat <<
    (whitespace? << closing_parenthesis.ignore).maybe
end

#prefixObject



90
91
92
93
94
# File 'lib/code/parser/function.rb', line 90

def prefix
  (asterisk << asterisk).aka(:keyword_splat) |
    asterisk.aka(:regular_splat) | ampersand.aka(:block) |
    spread_operator.aka(:spread)
end

#regular_parameterObject



101
102
103
104
# File 'lib/code/parser/function.rb', line 101

def regular_parameter
  ((prefix.maybe << name.aka(:name)) | prefix) << whitespace? <<
    (equal << whitespace? << code_present.aka(:default)).maybe
end

#rootObject



127
128
129
130
131
132
# File 'lib/code/parser/function.rb', line 127

def root
  (
    parameters.aka(:parameters) << whitespace? << equal << greater <<
      whitespace? << body.aka(:body)
  ).aka(:function) | Dictionary
end

#separatorObject



30
31
32
# File 'lib/code/parser/function.rb', line 30

def separator
  Name.new.separator
end

#spread_operatorObject



46
47
48
# File 'lib/code/parser/function.rb', line 46

def spread_operator
  str("...") | str("..") | str(".")
end

#whitespaceObject



22
23
24
# File 'lib/code/parser/function.rb', line 22

def whitespace
  Whitespace
end

#whitespace?Boolean

Returns:



26
27
28
# File 'lib/code/parser/function.rb', line 26

def whitespace?
  whitespace.maybe
end