bel_parser 1.0.0.alpha.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gemspec +28 -0
- data/CHANGELOG.md +10 -0
- data/LICENSE +191 -0
- data/README.md +9 -0
- data/VERSION +1 -0
- data/bin/bel2_termcheck +39 -0
- data/lib/bel_parser.rb +17 -0
- data/lib/bel_parser/ast_filter.rb +27 -0
- data/lib/bel_parser/ast_generator.rb +86 -0
- data/lib/bel_parser/ast_validator.rb +40 -0
- data/lib/bel_parser/expression/parser.rb +42 -0
- data/lib/bel_parser/expression/term_semantics.rb +36 -0
- data/lib/bel_parser/language.rb +7 -0
- data/lib/bel_parser/language/function.rb +59 -0
- data/lib/bel_parser/language/quoting.rb +236 -0
- data/lib/bel_parser/language/semantic_ast.rb +604 -0
- data/lib/bel_parser/language/semantics/analyzer.rb +59 -0
- data/lib/bel_parser/language/signature.rb +39 -0
- data/lib/bel_parser/language/specification.rb +49 -0
- data/lib/bel_parser/language/syntax/expression/incomplete_node.rb +14 -0
- data/lib/bel_parser/language/syntax/expression/invalid_term_function.rb +22 -0
- data/lib/bel_parser/language/version1.rb +50 -0
- data/lib/bel_parser/language/version1/functions/abundance.rb +85 -0
- data/lib/bel_parser/language/version1/functions/biological_process.rb +85 -0
- data/lib/bel_parser/language/version1/functions/catalytic_activity.rb +110 -0
- data/lib/bel_parser/language/version1/functions/cell_secretion.rb +80 -0
- data/lib/bel_parser/language/version1/functions/cell_surface_expression.rb +80 -0
- data/lib/bel_parser/language/version1/functions/chaperone_activity.rb +110 -0
- data/lib/bel_parser/language/version1/functions/complex_abundance.rb +115 -0
- data/lib/bel_parser/language/version1/functions/composite_abundance.rb +80 -0
- data/lib/bel_parser/language/version1/functions/degradation.rb +80 -0
- data/lib/bel_parser/language/version1/functions/fusion.rb +302 -0
- data/lib/bel_parser/language/version1/functions/gene_abundance.rb +125 -0
- data/lib/bel_parser/language/version1/functions/gtp_bound_activity.rb +110 -0
- data/lib/bel_parser/language/version1/functions/kinase_activity.rb +110 -0
- data/lib/bel_parser/language/version1/functions/list.rb +115 -0
- data/lib/bel_parser/language/version1/functions/micro_rna_abundance.rb +85 -0
- data/lib/bel_parser/language/version1/functions/molecular_activity.rb +80 -0
- data/lib/bel_parser/language/version1/functions/pathology.rb +85 -0
- data/lib/bel_parser/language/version1/functions/peptidase_activity.rb +110 -0
- data/lib/bel_parser/language/version1/functions/phosphatase_activity.rb +110 -0
- data/lib/bel_parser/language/version1/functions/products.rb +80 -0
- data/lib/bel_parser/language/version1/functions/protein_abundance.rb +245 -0
- data/lib/bel_parser/language/version1/functions/protein_modification.rb +167 -0
- data/lib/bel_parser/language/version1/functions/reactants.rb +80 -0
- data/lib/bel_parser/language/version1/functions/reaction.rb +85 -0
- data/lib/bel_parser/language/version1/functions/ribosylation_activity.rb +110 -0
- data/lib/bel_parser/language/version1/functions/rna_abundance.rb +125 -0
- data/lib/bel_parser/language/version1/functions/substitution.rb +96 -0
- data/lib/bel_parser/language/version1/functions/transcriptional_activity.rb +110 -0
- data/lib/bel_parser/language/version1/functions/translocation.rb +100 -0
- data/lib/bel_parser/language/version1/functions/transport_activity.rb +110 -0
- data/lib/bel_parser/language/version1/functions/truncation.rb +82 -0
- data/lib/bel_parser/language/version1/return_types/abundance.rb +20 -0
- data/lib/bel_parser/language/version1/return_types/any.rb +74 -0
- data/lib/bel_parser/language/version1/return_types/biological_process.rb +17 -0
- data/lib/bel_parser/language/version1/return_types/catalytic_activity.rb +20 -0
- data/lib/bel_parser/language/version1/return_types/chaperone_activity.rb +20 -0
- data/lib/bel_parser/language/version1/return_types/complex_abundance.rb +17 -0
- data/lib/bel_parser/language/version1/return_types/fusion.rb +17 -0
- data/lib/bel_parser/language/version1/return_types/gene_abundance.rb +17 -0
- data/lib/bel_parser/language/version1/return_types/gtp_bound_activity.rb +20 -0
- data/lib/bel_parser/language/version1/return_types/kinase_activity.rb +20 -0
- data/lib/bel_parser/language/version1/return_types/list.rb +17 -0
- data/lib/bel_parser/language/version1/return_types/micro_rna_abundance.rb +17 -0
- data/lib/bel_parser/language/version1/return_types/molecular_activity.rb +20 -0
- data/lib/bel_parser/language/version1/return_types/pathology.rb +17 -0
- data/lib/bel_parser/language/version1/return_types/peptidase_activity.rb +20 -0
- data/lib/bel_parser/language/version1/return_types/phosphatase_activity.rb +20 -0
- data/lib/bel_parser/language/version1/return_types/products.rb +17 -0
- data/lib/bel_parser/language/version1/return_types/protein_abundance.rb +17 -0
- data/lib/bel_parser/language/version1/return_types/protein_modification.rb +17 -0
- data/lib/bel_parser/language/version1/return_types/reactants.rb +17 -0
- data/lib/bel_parser/language/version1/return_types/ribosylation_activity.rb +20 -0
- data/lib/bel_parser/language/version1/return_types/rna_abundance.rb +17 -0
- data/lib/bel_parser/language/version1/return_types/substitution.rb +17 -0
- data/lib/bel_parser/language/version1/return_types/transcriptional_activity.rb +20 -0
- data/lib/bel_parser/language/version1/return_types/transport_activity.rb +20 -0
- data/lib/bel_parser/language/version1/return_types/truncation.rb +17 -0
- data/lib/bel_parser/language/version2.rb +50 -0
- data/lib/bel_parser/language/version2/functions/abundance.rb +165 -0
- data/lib/bel_parser/language/version2/functions/activity.rb +115 -0
- data/lib/bel_parser/language/version2/functions/biological_process.rb +85 -0
- data/lib/bel_parser/language/version2/functions/cell_secretion.rb +80 -0
- data/lib/bel_parser/language/version2/functions/cell_surface_expression.rb +80 -0
- data/lib/bel_parser/language/version2/functions/complex_abundance.rb +190 -0
- data/lib/bel_parser/language/version2/functions/composite_abundance.rb +80 -0
- data/lib/bel_parser/language/version2/functions/degradation.rb +80 -0
- data/lib/bel_parser/language/version2/functions/fragment.rb +119 -0
- data/lib/bel_parser/language/version2/functions/from_location.rb +85 -0
- data/lib/bel_parser/language/version2/functions/fusion.rb +227 -0
- data/lib/bel_parser/language/version2/functions/gene_abundance.rb +195 -0
- data/lib/bel_parser/language/version2/functions/list.rb +115 -0
- data/lib/bel_parser/language/version2/functions/location.rb +85 -0
- data/lib/bel_parser/language/version2/functions/micro_rna_abundance.rb +165 -0
- data/lib/bel_parser/language/version2/functions/molecular_activity.rb +83 -0
- data/lib/bel_parser/language/version2/functions/pathology.rb +85 -0
- data/lib/bel_parser/language/version2/functions/products.rb +80 -0
- data/lib/bel_parser/language/version2/functions/protein_abundance.rb +285 -0
- data/lib/bel_parser/language/version2/functions/protein_modification.rb +167 -0
- data/lib/bel_parser/language/version2/functions/reactants.rb +80 -0
- data/lib/bel_parser/language/version2/functions/reaction.rb +85 -0
- data/lib/bel_parser/language/version2/functions/rna_abundance.rb +195 -0
- data/lib/bel_parser/language/version2/functions/to_location.rb +85 -0
- data/lib/bel_parser/language/version2/functions/translocation.rb +90 -0
- data/lib/bel_parser/language/version2/functions/variant.rb +83 -0
- data/lib/bel_parser/language/version2/return_types/abundance.rb +20 -0
- data/lib/bel_parser/language/version2/return_types/activity.rb +20 -0
- data/lib/bel_parser/language/version2/return_types/any.rb +74 -0
- data/lib/bel_parser/language/version2/return_types/biological_process.rb +17 -0
- data/lib/bel_parser/language/version2/return_types/complex_abundance.rb +17 -0
- data/lib/bel_parser/language/version2/return_types/fragment.rb +20 -0
- data/lib/bel_parser/language/version2/return_types/from_location.rb +20 -0
- data/lib/bel_parser/language/version2/return_types/fusion.rb +17 -0
- data/lib/bel_parser/language/version2/return_types/gene_abundance.rb +17 -0
- data/lib/bel_parser/language/version2/return_types/list.rb +17 -0
- data/lib/bel_parser/language/version2/return_types/location.rb +20 -0
- data/lib/bel_parser/language/version2/return_types/micro_rna_abundance.rb +17 -0
- data/lib/bel_parser/language/version2/return_types/molecular_activity.rb +20 -0
- data/lib/bel_parser/language/version2/return_types/pathology.rb +17 -0
- data/lib/bel_parser/language/version2/return_types/products.rb +17 -0
- data/lib/bel_parser/language/version2/return_types/protein_abundance.rb +17 -0
- data/lib/bel_parser/language/version2/return_types/protein_modification.rb +17 -0
- data/lib/bel_parser/language/version2/return_types/reactants.rb +17 -0
- data/lib/bel_parser/language/version2/return_types/rna_abundance.rb +17 -0
- data/lib/bel_parser/language/version2/return_types/to_location.rb +20 -0
- data/lib/bel_parser/language/version2/return_types/variant.rb +20 -0
- data/lib/bel_parser/mixin/line_continuator.rb +15 -0
- data/lib/bel_parser/mixin/line_mapping.rb +14 -0
- data/lib/bel_parser/parser.rb +54 -0
- data/lib/bel_parser/parsers/ast/mapped_traversal.rb +36 -0
- data/lib/bel_parser/parsers/ast/node.rb +705 -0
- data/lib/bel_parser/parsers/ast/sexp.rb +8 -0
- data/lib/bel_parser/parsers/ast/traversal.rb +21 -0
- data/lib/bel_parser/parsers/bel_script.rb +4 -0
- data/lib/bel_parser/parsers/bel_script/define_annotation.rb +5476 -0
- data/lib/bel_parser/parsers/bel_script/define_annotation.rl +141 -0
- data/lib/bel_parser/parsers/bel_script/define_namespace.rb +1780 -0
- data/lib/bel_parser/parsers/bel_script/define_namespace.rl +121 -0
- data/lib/bel_parser/parsers/bel_script/set.rb +4556 -0
- data/lib/bel_parser/parsers/bel_script/set.rl +116 -0
- data/lib/bel_parser/parsers/bel_script/unset.rb +706 -0
- data/lib/bel_parser/parsers/bel_script/unset.rl +95 -0
- data/lib/bel_parser/parsers/common.rb +5 -0
- data/lib/bel_parser/parsers/common/blank_line.rb +211 -0
- data/lib/bel_parser/parsers/common/blank_line.rl +81 -0
- data/lib/bel_parser/parsers/common/comment_line.rb +245 -0
- data/lib/bel_parser/parsers/common/comment_line.rl +97 -0
- data/lib/bel_parser/parsers/common/common.rb +7 -0
- data/lib/bel_parser/parsers/common/common.rl +13 -0
- data/lib/bel_parser/parsers/common/identifier.rb +289 -0
- data/lib/bel_parser/parsers/common/identifier.rl +106 -0
- data/lib/bel_parser/parsers/common/list.rb +2142 -0
- data/lib/bel_parser/parsers/common/list.rl +144 -0
- data/lib/bel_parser/parsers/common/string.rb +271 -0
- data/lib/bel_parser/parsers/common/string.rl +107 -0
- data/lib/bel_parser/parsers/expression.rb +7 -0
- data/lib/bel_parser/parsers/expression/comment.rb +239 -0
- data/lib/bel_parser/parsers/expression/comment.rl +97 -0
- data/lib/bel_parser/parsers/expression/parameter.rb +1506 -0
- data/lib/bel_parser/parsers/expression/parameter.rl +97 -0
- data/lib/bel_parser/parsers/expression/relationship.rb +254 -0
- data/lib/bel_parser/parsers/expression/relationship.rl +98 -0
- data/lib/bel_parser/parsers/expression/statement_nested.rb +17802 -0
- data/lib/bel_parser/parsers/expression/statement_nested.rl +141 -0
- data/lib/bel_parser/parsers/expression/statement_observed_term.rb +7291 -0
- data/lib/bel_parser/parsers/expression/statement_observed_term.rl +92 -0
- data/lib/bel_parser/parsers/expression/statement_simple.rb +10475 -0
- data/lib/bel_parser/parsers/expression/statement_simple.rl +112 -0
- data/lib/bel_parser/parsers/expression/term.rb +3989 -0
- data/lib/bel_parser/parsers/expression/term.rl +157 -0
- data/lib/bel_parser/parsers/line_parser.rb +92 -0
- data/lib/bel_parser/parsers/mixin/buffer.rb +10 -0
- data/lib/bel_parser/parsers/nonblocking_io_wrapper.rb +50 -0
- data/lib/bel_parser/script/parser.rb +49 -0
- data/lib/bel_parser/vendor/ast.rb +17 -0
- data/lib/bel_parser/vendor/ast/node.rb +254 -0
- data/lib/bel_parser/vendor/ast/processor.rb +12 -0
- data/lib/bel_parser/vendor/ast/processor/mixin.rb +282 -0
- data/lib/bel_parser/vendor/ast/sexp.rb +30 -0
- metadata +226 -0
@@ -0,0 +1,12 @@
|
|
1
|
+
module AST
|
2
|
+
# This class includes {AST::Processor::Mixin}; however, it is
|
3
|
+
# deprecated, since the module defines all of the behaviors that
|
4
|
+
# the processor includes. Any new libraries should use
|
5
|
+
# {AST::Processor::Mixin} instead of subclassing this.
|
6
|
+
#
|
7
|
+
# @deprecated Use {AST::Processor::Mixin} instead.
|
8
|
+
class Processor
|
9
|
+
require_relative 'processor/mixin'
|
10
|
+
include Mixin
|
11
|
+
end
|
12
|
+
end
|
@@ -0,0 +1,282 @@
|
|
1
|
+
module AST
|
2
|
+
class Processor
|
3
|
+
# The processor module is a module which helps transforming one
|
4
|
+
# AST into another. In a nutshell, the {#process} method accepts
|
5
|
+
# a {Node} and dispatches it to a handler corresponding to its
|
6
|
+
# type, and returns a (possibly) updated variant of the node.
|
7
|
+
#
|
8
|
+
# The processor module has a set of associated design patterns.
|
9
|
+
# They are best explained with a concrete example. Let's define a
|
10
|
+
# simple arithmetic language and an AST format for it:
|
11
|
+
#
|
12
|
+
# Terminals (AST nodes which do not have other AST nodes inside):
|
13
|
+
#
|
14
|
+
# * `(integer <int-literal>)`,
|
15
|
+
#
|
16
|
+
# Nonterminals (AST nodes with other nodes as children):
|
17
|
+
#
|
18
|
+
# * `(add <node> <node>)`,
|
19
|
+
# * `(multiply <node> <node>)`,
|
20
|
+
# * `(divide <node> <node>)`,
|
21
|
+
# * `(negate <node>)`,
|
22
|
+
# * `(store <node> <string-literal>)`: stores value of `<node>`
|
23
|
+
# into a variable named `<string-literal>`,
|
24
|
+
# * `(load <string-literal>)`: loads value of a variable named
|
25
|
+
# `<string-literal>`,
|
26
|
+
# * `(each <node> ...): computes each of the `<node>`s and
|
27
|
+
# prints the result.
|
28
|
+
#
|
29
|
+
# All AST nodes have the same Ruby class, and therefore they don't
|
30
|
+
# know how to traverse themselves. (A solution which dynamically
|
31
|
+
# checks the type of children is possible, but is slow and
|
32
|
+
# error-prone.) So, a class including the module which knows how
|
33
|
+
# to traverse the entire tree should be defined. Such classes
|
34
|
+
# have a handler for each nonterminal node which recursively
|
35
|
+
# processes children nodes:
|
36
|
+
#
|
37
|
+
# require 'ast'
|
38
|
+
#
|
39
|
+
# class ArithmeticsProcessor
|
40
|
+
# include AST::Processor::Module
|
41
|
+
# # This method traverses any binary operators such as (add)
|
42
|
+
# # or (multiply).
|
43
|
+
# def process_binary_op(node)
|
44
|
+
# # Children aren't decomposed automatically; it is
|
45
|
+
# # suggested to use Ruby multiple assignment expansion,
|
46
|
+
# # as it is very convenient here.
|
47
|
+
# left_expr, right_expr = *node
|
48
|
+
#
|
49
|
+
# # AST::Node#updated won't change node type if nil is
|
50
|
+
# # passed as a first argument, which allows to reuse the
|
51
|
+
# # same handler for multiple node types using `alias'
|
52
|
+
# # (below).
|
53
|
+
# node.updated(nil, [
|
54
|
+
# process(left_expr),
|
55
|
+
# process(right_expr)
|
56
|
+
# ])
|
57
|
+
# end
|
58
|
+
# alias_method :on_add, :process_binary_op
|
59
|
+
# alias_method :on_multiply, :process_binary_op
|
60
|
+
# alias_method :on_divide, :process_binary_op
|
61
|
+
#
|
62
|
+
# def on_negate(node)
|
63
|
+
# # It is also possible to use #process_all for more
|
64
|
+
# # compact code if every child is a Node.
|
65
|
+
# node.updated(nil, process_all(node))
|
66
|
+
# end
|
67
|
+
#
|
68
|
+
# def on_store(node)
|
69
|
+
# expr, variable_name = *node
|
70
|
+
#
|
71
|
+
# # Note that variable_name is not a Node and thus isn't
|
72
|
+
# # passed to #process.
|
73
|
+
# node.updated(nil, [
|
74
|
+
# process(expr),
|
75
|
+
# variable_name
|
76
|
+
# ])
|
77
|
+
# end
|
78
|
+
#
|
79
|
+
# # (load) is effectively a terminal node, and so it does
|
80
|
+
# # not need an explicit handler, as the following is the
|
81
|
+
# # default behavior. Essentially, for any nodes that don't
|
82
|
+
# # have a defined handler, the node remains unchanged.
|
83
|
+
# def on_load(node)
|
84
|
+
# nil
|
85
|
+
# end
|
86
|
+
#
|
87
|
+
# def on_each(node)
|
88
|
+
# node.updated(nil, process_all(node))
|
89
|
+
# end
|
90
|
+
# end
|
91
|
+
#
|
92
|
+
# Let's test our ArithmeticsProcessor:
|
93
|
+
#
|
94
|
+
# include AST::Sexp
|
95
|
+
# expr = s(:add, s(:integer, 2), s(:integer, 2))
|
96
|
+
#
|
97
|
+
# p ArithmeticsProcessor.new.process(expr) == expr # => true
|
98
|
+
#
|
99
|
+
# As expected, it does not change anything at all. This isn't
|
100
|
+
# actually very useful, so let's now define a Calculator, which
|
101
|
+
# will compute the expression values:
|
102
|
+
#
|
103
|
+
# # This Processor folds nonterminal nodes and returns an
|
104
|
+
# # (integer) terminal node.
|
105
|
+
# class ArithmeticsCalculator < ArithmeticsProcessor
|
106
|
+
# def compute_op(node)
|
107
|
+
# # First, node children are processed and then unpacked
|
108
|
+
# # to local variables.
|
109
|
+
# nodes = process_all(node)
|
110
|
+
#
|
111
|
+
# if nodes.all? { |node| node.type == :integer }
|
112
|
+
# # If each of those nodes represents a literal, we can
|
113
|
+
# # fold this node!
|
114
|
+
# values = nodes.map { |node| node.children.first }
|
115
|
+
# AST::Node.new(:integer, [
|
116
|
+
# yield(values)
|
117
|
+
# ])
|
118
|
+
# else
|
119
|
+
# # Otherwise, we can just leave the current node in the
|
120
|
+
# # tree and only update it with processed children
|
121
|
+
# # nodes, which can be partially folded.
|
122
|
+
# node.updated(nil, nodes)
|
123
|
+
# end
|
124
|
+
# end
|
125
|
+
#
|
126
|
+
# def on_add(node)
|
127
|
+
# compute_op(node) { |left, right| left + right }
|
128
|
+
# end
|
129
|
+
#
|
130
|
+
# def on_multiply(node)
|
131
|
+
# compute_op(node) { |left, right| left * right }
|
132
|
+
# end
|
133
|
+
# end
|
134
|
+
#
|
135
|
+
# Let's check:
|
136
|
+
#
|
137
|
+
# p ArithmeticsCalculator.new.process(expr) # => (integer 4)
|
138
|
+
#
|
139
|
+
# Excellent, the calculator works! Now, a careful reader could
|
140
|
+
# notice that the ArithmeticsCalculator does not know how to
|
141
|
+
# divide numbers. What if we pass an expression with division to
|
142
|
+
# it?
|
143
|
+
#
|
144
|
+
# expr_with_division = \
|
145
|
+
# s(:add,
|
146
|
+
# s(:integer, 1),
|
147
|
+
# s(:divide,
|
148
|
+
# s(:add, s(:integer, 8), s(:integer, 4)),
|
149
|
+
# s(:integer, 3))) # 1 + (8 + 4) / 3
|
150
|
+
#
|
151
|
+
# folded_expr_with_division = ArithmeticsCalculator.new.process(expr_with_division)
|
152
|
+
# p folded_expr_with_division
|
153
|
+
# # => (add
|
154
|
+
# # (integer 1)
|
155
|
+
# # (divide
|
156
|
+
# # (integer 12)
|
157
|
+
# # (integer 3)))
|
158
|
+
#
|
159
|
+
# As you can see, the expression was folded _partially_: the inner
|
160
|
+
# `(add)` node which could be computed was folded to
|
161
|
+
# `(integer 12)`, the `(divide)` node is left as-is because there
|
162
|
+
# is no computing handler for it, and the root `(add)` node was
|
163
|
+
# also left as it is because some of its children were not
|
164
|
+
# literals.
|
165
|
+
#
|
166
|
+
# Note that this partial folding is only possible because the
|
167
|
+
# _data_ format, i.e. the format in which the computed values of
|
168
|
+
# the nodes are represented, is the same as the AST itself. # # Let's extend our ArithmeticsCalculator class further. # # class ArithmeticsCalculator # def on_divide(node) # compute_op(node) { |left, right| left / right }
|
169
|
+
# end
|
170
|
+
#
|
171
|
+
# def on_negate(node)
|
172
|
+
# # Note how #compute_op works regardless of the operator
|
173
|
+
# # arity.
|
174
|
+
# compute_op(node) { |value| -value }
|
175
|
+
# end
|
176
|
+
# end
|
177
|
+
#
|
178
|
+
# Now, let's apply our renewed ArithmeticsCalculator to a partial
|
179
|
+
# result of previous evaluation:
|
180
|
+
#
|
181
|
+
# p ArithmeticsCalculator.new.process(expr_with_division) # => (integer 5)
|
182
|
+
#
|
183
|
+
# Five! Excellent. This is also pretty much how CRuby 1.8 executed
|
184
|
+
# its programs.
|
185
|
+
#
|
186
|
+
# Now, let's do some automated bug searching. Division by zero is
|
187
|
+
# an error, right? So if we could detect that someone has divided
|
188
|
+
# by zero before the program is even run, that could save some
|
189
|
+
# debugging time.
|
190
|
+
#
|
191
|
+
# class DivisionByZeroVerifier < ArithmeticsProcessor
|
192
|
+
# class VerificationFailure < Exception; end
|
193
|
+
#
|
194
|
+
# def on_divide(node)
|
195
|
+
# # You need to process the children to handle nested divisions
|
196
|
+
# # such as:
|
197
|
+
# # (divide
|
198
|
+
# # (integer 1)
|
199
|
+
# # (divide (integer 1) (integer 0))
|
200
|
+
# left, right = process_all(node)
|
201
|
+
#
|
202
|
+
# if right.type == :integer &&
|
203
|
+
# right.children.first == 0
|
204
|
+
# raise VerificationFailure, "Ouch! This code divides by zero."
|
205
|
+
# end
|
206
|
+
# end
|
207
|
+
#
|
208
|
+
# def divides_by_zero?(ast)
|
209
|
+
# process(ast)
|
210
|
+
# false
|
211
|
+
# rescue VerificationFailure
|
212
|
+
# true
|
213
|
+
# end
|
214
|
+
# end
|
215
|
+
#
|
216
|
+
# nice_expr = \
|
217
|
+
# s(:divide,
|
218
|
+
# s(:add, s(:integer, 10), s(:integer, 2)),
|
219
|
+
# s(:integer, 4))
|
220
|
+
#
|
221
|
+
# p DivisionByZeroVerifier.new.divides_by_zero?(nice_expr)
|
222
|
+
# # => false. Good.
|
223
|
+
#
|
224
|
+
# bad_expr = \
|
225
|
+
# s(:add, s(:integer, 10),
|
226
|
+
# s(:divide, s(:integer, 1), s(:integer, 0)))
|
227
|
+
#
|
228
|
+
# p DivisionByZeroVerifier.new.divides_by_zero?(bad_expr)
|
229
|
+
# # => true. WHOOPS. DO NOT RUN THIS.
|
230
|
+
#
|
231
|
+
# Of course, this won't detect more complex cases... unless you
|
232
|
+
# use some partial evaluation before! The possibilites are
|
233
|
+
# endless. Have fun.
|
234
|
+
module Mixin
|
235
|
+
# Dispatches `node`. If a node has type `:foo`, then a handler
|
236
|
+
# named `on_foo` is invoked with one argument, the `node`; if
|
237
|
+
# there isn't such a handler, {#handler_missing} is invoked
|
238
|
+
# with the same argument.
|
239
|
+
#
|
240
|
+
# If the handler returns `nil`, `node` is returned; otherwise,
|
241
|
+
# the return value of the handler is passed along.
|
242
|
+
#
|
243
|
+
# @param [AST::Node, nil] node
|
244
|
+
# @return [AST::Node, nil]
|
245
|
+
def process(node)
|
246
|
+
return if node.nil?
|
247
|
+
|
248
|
+
node = node.to_ast
|
249
|
+
|
250
|
+
# Invoke a specific handler
|
251
|
+
on_handler = :"on_#{node.type}"
|
252
|
+
if respond_to? on_handler
|
253
|
+
new_node = send on_handler, node
|
254
|
+
else
|
255
|
+
new_node = handler_missing(node)
|
256
|
+
end
|
257
|
+
|
258
|
+
node = new_node if new_node
|
259
|
+
|
260
|
+
node
|
261
|
+
end
|
262
|
+
|
263
|
+
# {#process}es each node from `nodes` and returns an array of
|
264
|
+
# results.
|
265
|
+
#
|
266
|
+
# @param [Array<AST::Node>] nodes
|
267
|
+
# @return [Array<AST::Node>]
|
268
|
+
def process_all(nodes)
|
269
|
+
nodes.to_a.map do |node|
|
270
|
+
process node
|
271
|
+
end
|
272
|
+
end
|
273
|
+
|
274
|
+
# Default handler. Does nothing.
|
275
|
+
#
|
276
|
+
# @param [AST::Node] node
|
277
|
+
# @return [AST::Node, nil]
|
278
|
+
def handler_missing(node)
|
279
|
+
end
|
280
|
+
end
|
281
|
+
end
|
282
|
+
end
|
@@ -0,0 +1,30 @@
|
|
1
|
+
module AST
|
2
|
+
# This simple module is very useful in the cases where one needs
|
3
|
+
# to define deeply nested ASTs from Ruby code, for example, in
|
4
|
+
# tests. It should be used like this:
|
5
|
+
#
|
6
|
+
# describe YourLanguage::AST do
|
7
|
+
# include Sexp
|
8
|
+
#
|
9
|
+
# it "should correctly parse expressions" do
|
10
|
+
# YourLanguage.parse("1 + 2 * 3").should ==
|
11
|
+
# s(:add,
|
12
|
+
# s(:integer, 1),
|
13
|
+
# s(:multiply,
|
14
|
+
# s(:integer, 2),
|
15
|
+
# s(:integer, 3)))
|
16
|
+
# end
|
17
|
+
# end
|
18
|
+
#
|
19
|
+
# This way the amount of boilerplate code is greatly reduced.
|
20
|
+
module Sexp
|
21
|
+
# Creates a {Node} with type `type` and children `children`.
|
22
|
+
# Note that the resulting node is of the type AST::Node and not a
|
23
|
+
# subclass.
|
24
|
+
# This would not pose a problem with comparisons, as {Node#==}
|
25
|
+
# ignores metadata.
|
26
|
+
def s(type, *children)
|
27
|
+
Node.new(type, children)
|
28
|
+
end
|
29
|
+
end
|
30
|
+
end
|
metadata
ADDED
@@ -0,0 +1,226 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: bel_parser
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 1.0.0.alpha.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Anthony Bargnesi
|
8
|
+
- Nick Bargnesi
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2016-04-05 00:00:00.000000000 Z
|
13
|
+
dependencies: []
|
14
|
+
description: Implements language versions 1.0 and 2.0.
|
15
|
+
email: abargnesi@selventa.com
|
16
|
+
executables:
|
17
|
+
- bel2_termcheck
|
18
|
+
extensions: []
|
19
|
+
extra_rdoc_files: []
|
20
|
+
files:
|
21
|
+
- ".gemspec"
|
22
|
+
- CHANGELOG.md
|
23
|
+
- LICENSE
|
24
|
+
- README.md
|
25
|
+
- VERSION
|
26
|
+
- bin/bel2_termcheck
|
27
|
+
- lib/bel_parser.rb
|
28
|
+
- lib/bel_parser/ast_filter.rb
|
29
|
+
- lib/bel_parser/ast_generator.rb
|
30
|
+
- lib/bel_parser/ast_validator.rb
|
31
|
+
- lib/bel_parser/expression/parser.rb
|
32
|
+
- lib/bel_parser/expression/term_semantics.rb
|
33
|
+
- lib/bel_parser/language.rb
|
34
|
+
- lib/bel_parser/language/function.rb
|
35
|
+
- lib/bel_parser/language/quoting.rb
|
36
|
+
- lib/bel_parser/language/semantic_ast.rb
|
37
|
+
- lib/bel_parser/language/semantics/analyzer.rb
|
38
|
+
- lib/bel_parser/language/signature.rb
|
39
|
+
- lib/bel_parser/language/specification.rb
|
40
|
+
- lib/bel_parser/language/syntax/expression/incomplete_node.rb
|
41
|
+
- lib/bel_parser/language/syntax/expression/invalid_term_function.rb
|
42
|
+
- lib/bel_parser/language/version1.rb
|
43
|
+
- lib/bel_parser/language/version1/functions/abundance.rb
|
44
|
+
- lib/bel_parser/language/version1/functions/biological_process.rb
|
45
|
+
- lib/bel_parser/language/version1/functions/catalytic_activity.rb
|
46
|
+
- lib/bel_parser/language/version1/functions/cell_secretion.rb
|
47
|
+
- lib/bel_parser/language/version1/functions/cell_surface_expression.rb
|
48
|
+
- lib/bel_parser/language/version1/functions/chaperone_activity.rb
|
49
|
+
- lib/bel_parser/language/version1/functions/complex_abundance.rb
|
50
|
+
- lib/bel_parser/language/version1/functions/composite_abundance.rb
|
51
|
+
- lib/bel_parser/language/version1/functions/degradation.rb
|
52
|
+
- lib/bel_parser/language/version1/functions/fusion.rb
|
53
|
+
- lib/bel_parser/language/version1/functions/gene_abundance.rb
|
54
|
+
- lib/bel_parser/language/version1/functions/gtp_bound_activity.rb
|
55
|
+
- lib/bel_parser/language/version1/functions/kinase_activity.rb
|
56
|
+
- lib/bel_parser/language/version1/functions/list.rb
|
57
|
+
- lib/bel_parser/language/version1/functions/micro_rna_abundance.rb
|
58
|
+
- lib/bel_parser/language/version1/functions/molecular_activity.rb
|
59
|
+
- lib/bel_parser/language/version1/functions/pathology.rb
|
60
|
+
- lib/bel_parser/language/version1/functions/peptidase_activity.rb
|
61
|
+
- lib/bel_parser/language/version1/functions/phosphatase_activity.rb
|
62
|
+
- lib/bel_parser/language/version1/functions/products.rb
|
63
|
+
- lib/bel_parser/language/version1/functions/protein_abundance.rb
|
64
|
+
- lib/bel_parser/language/version1/functions/protein_modification.rb
|
65
|
+
- lib/bel_parser/language/version1/functions/reactants.rb
|
66
|
+
- lib/bel_parser/language/version1/functions/reaction.rb
|
67
|
+
- lib/bel_parser/language/version1/functions/ribosylation_activity.rb
|
68
|
+
- lib/bel_parser/language/version1/functions/rna_abundance.rb
|
69
|
+
- lib/bel_parser/language/version1/functions/substitution.rb
|
70
|
+
- lib/bel_parser/language/version1/functions/transcriptional_activity.rb
|
71
|
+
- lib/bel_parser/language/version1/functions/translocation.rb
|
72
|
+
- lib/bel_parser/language/version1/functions/transport_activity.rb
|
73
|
+
- lib/bel_parser/language/version1/functions/truncation.rb
|
74
|
+
- lib/bel_parser/language/version1/return_types/abundance.rb
|
75
|
+
- lib/bel_parser/language/version1/return_types/any.rb
|
76
|
+
- lib/bel_parser/language/version1/return_types/biological_process.rb
|
77
|
+
- lib/bel_parser/language/version1/return_types/catalytic_activity.rb
|
78
|
+
- lib/bel_parser/language/version1/return_types/chaperone_activity.rb
|
79
|
+
- lib/bel_parser/language/version1/return_types/complex_abundance.rb
|
80
|
+
- lib/bel_parser/language/version1/return_types/fusion.rb
|
81
|
+
- lib/bel_parser/language/version1/return_types/gene_abundance.rb
|
82
|
+
- lib/bel_parser/language/version1/return_types/gtp_bound_activity.rb
|
83
|
+
- lib/bel_parser/language/version1/return_types/kinase_activity.rb
|
84
|
+
- lib/bel_parser/language/version1/return_types/list.rb
|
85
|
+
- lib/bel_parser/language/version1/return_types/micro_rna_abundance.rb
|
86
|
+
- lib/bel_parser/language/version1/return_types/molecular_activity.rb
|
87
|
+
- lib/bel_parser/language/version1/return_types/pathology.rb
|
88
|
+
- lib/bel_parser/language/version1/return_types/peptidase_activity.rb
|
89
|
+
- lib/bel_parser/language/version1/return_types/phosphatase_activity.rb
|
90
|
+
- lib/bel_parser/language/version1/return_types/products.rb
|
91
|
+
- lib/bel_parser/language/version1/return_types/protein_abundance.rb
|
92
|
+
- lib/bel_parser/language/version1/return_types/protein_modification.rb
|
93
|
+
- lib/bel_parser/language/version1/return_types/reactants.rb
|
94
|
+
- lib/bel_parser/language/version1/return_types/ribosylation_activity.rb
|
95
|
+
- lib/bel_parser/language/version1/return_types/rna_abundance.rb
|
96
|
+
- lib/bel_parser/language/version1/return_types/substitution.rb
|
97
|
+
- lib/bel_parser/language/version1/return_types/transcriptional_activity.rb
|
98
|
+
- lib/bel_parser/language/version1/return_types/transport_activity.rb
|
99
|
+
- lib/bel_parser/language/version1/return_types/truncation.rb
|
100
|
+
- lib/bel_parser/language/version2.rb
|
101
|
+
- lib/bel_parser/language/version2/functions/abundance.rb
|
102
|
+
- lib/bel_parser/language/version2/functions/activity.rb
|
103
|
+
- lib/bel_parser/language/version2/functions/biological_process.rb
|
104
|
+
- lib/bel_parser/language/version2/functions/cell_secretion.rb
|
105
|
+
- lib/bel_parser/language/version2/functions/cell_surface_expression.rb
|
106
|
+
- lib/bel_parser/language/version2/functions/complex_abundance.rb
|
107
|
+
- lib/bel_parser/language/version2/functions/composite_abundance.rb
|
108
|
+
- lib/bel_parser/language/version2/functions/degradation.rb
|
109
|
+
- lib/bel_parser/language/version2/functions/fragment.rb
|
110
|
+
- lib/bel_parser/language/version2/functions/from_location.rb
|
111
|
+
- lib/bel_parser/language/version2/functions/fusion.rb
|
112
|
+
- lib/bel_parser/language/version2/functions/gene_abundance.rb
|
113
|
+
- lib/bel_parser/language/version2/functions/list.rb
|
114
|
+
- lib/bel_parser/language/version2/functions/location.rb
|
115
|
+
- lib/bel_parser/language/version2/functions/micro_rna_abundance.rb
|
116
|
+
- lib/bel_parser/language/version2/functions/molecular_activity.rb
|
117
|
+
- lib/bel_parser/language/version2/functions/pathology.rb
|
118
|
+
- lib/bel_parser/language/version2/functions/products.rb
|
119
|
+
- lib/bel_parser/language/version2/functions/protein_abundance.rb
|
120
|
+
- lib/bel_parser/language/version2/functions/protein_modification.rb
|
121
|
+
- lib/bel_parser/language/version2/functions/reactants.rb
|
122
|
+
- lib/bel_parser/language/version2/functions/reaction.rb
|
123
|
+
- lib/bel_parser/language/version2/functions/rna_abundance.rb
|
124
|
+
- lib/bel_parser/language/version2/functions/to_location.rb
|
125
|
+
- lib/bel_parser/language/version2/functions/translocation.rb
|
126
|
+
- lib/bel_parser/language/version2/functions/variant.rb
|
127
|
+
- lib/bel_parser/language/version2/return_types/abundance.rb
|
128
|
+
- lib/bel_parser/language/version2/return_types/activity.rb
|
129
|
+
- lib/bel_parser/language/version2/return_types/any.rb
|
130
|
+
- lib/bel_parser/language/version2/return_types/biological_process.rb
|
131
|
+
- lib/bel_parser/language/version2/return_types/complex_abundance.rb
|
132
|
+
- lib/bel_parser/language/version2/return_types/fragment.rb
|
133
|
+
- lib/bel_parser/language/version2/return_types/from_location.rb
|
134
|
+
- lib/bel_parser/language/version2/return_types/fusion.rb
|
135
|
+
- lib/bel_parser/language/version2/return_types/gene_abundance.rb
|
136
|
+
- lib/bel_parser/language/version2/return_types/list.rb
|
137
|
+
- lib/bel_parser/language/version2/return_types/location.rb
|
138
|
+
- lib/bel_parser/language/version2/return_types/micro_rna_abundance.rb
|
139
|
+
- lib/bel_parser/language/version2/return_types/molecular_activity.rb
|
140
|
+
- lib/bel_parser/language/version2/return_types/pathology.rb
|
141
|
+
- lib/bel_parser/language/version2/return_types/products.rb
|
142
|
+
- lib/bel_parser/language/version2/return_types/protein_abundance.rb
|
143
|
+
- lib/bel_parser/language/version2/return_types/protein_modification.rb
|
144
|
+
- lib/bel_parser/language/version2/return_types/reactants.rb
|
145
|
+
- lib/bel_parser/language/version2/return_types/rna_abundance.rb
|
146
|
+
- lib/bel_parser/language/version2/return_types/to_location.rb
|
147
|
+
- lib/bel_parser/language/version2/return_types/variant.rb
|
148
|
+
- lib/bel_parser/mixin/line_continuator.rb
|
149
|
+
- lib/bel_parser/mixin/line_mapping.rb
|
150
|
+
- lib/bel_parser/parser.rb
|
151
|
+
- lib/bel_parser/parsers/ast/mapped_traversal.rb
|
152
|
+
- lib/bel_parser/parsers/ast/node.rb
|
153
|
+
- lib/bel_parser/parsers/ast/sexp.rb
|
154
|
+
- lib/bel_parser/parsers/ast/traversal.rb
|
155
|
+
- lib/bel_parser/parsers/bel_script.rb
|
156
|
+
- lib/bel_parser/parsers/bel_script/define_annotation.rb
|
157
|
+
- lib/bel_parser/parsers/bel_script/define_annotation.rl
|
158
|
+
- lib/bel_parser/parsers/bel_script/define_namespace.rb
|
159
|
+
- lib/bel_parser/parsers/bel_script/define_namespace.rl
|
160
|
+
- lib/bel_parser/parsers/bel_script/set.rb
|
161
|
+
- lib/bel_parser/parsers/bel_script/set.rl
|
162
|
+
- lib/bel_parser/parsers/bel_script/unset.rb
|
163
|
+
- lib/bel_parser/parsers/bel_script/unset.rl
|
164
|
+
- lib/bel_parser/parsers/common.rb
|
165
|
+
- lib/bel_parser/parsers/common/blank_line.rb
|
166
|
+
- lib/bel_parser/parsers/common/blank_line.rl
|
167
|
+
- lib/bel_parser/parsers/common/comment_line.rb
|
168
|
+
- lib/bel_parser/parsers/common/comment_line.rl
|
169
|
+
- lib/bel_parser/parsers/common/common.rb
|
170
|
+
- lib/bel_parser/parsers/common/common.rl
|
171
|
+
- lib/bel_parser/parsers/common/identifier.rb
|
172
|
+
- lib/bel_parser/parsers/common/identifier.rl
|
173
|
+
- lib/bel_parser/parsers/common/list.rb
|
174
|
+
- lib/bel_parser/parsers/common/list.rl
|
175
|
+
- lib/bel_parser/parsers/common/string.rb
|
176
|
+
- lib/bel_parser/parsers/common/string.rl
|
177
|
+
- lib/bel_parser/parsers/expression.rb
|
178
|
+
- lib/bel_parser/parsers/expression/comment.rb
|
179
|
+
- lib/bel_parser/parsers/expression/comment.rl
|
180
|
+
- lib/bel_parser/parsers/expression/parameter.rb
|
181
|
+
- lib/bel_parser/parsers/expression/parameter.rl
|
182
|
+
- lib/bel_parser/parsers/expression/relationship.rb
|
183
|
+
- lib/bel_parser/parsers/expression/relationship.rl
|
184
|
+
- lib/bel_parser/parsers/expression/statement_nested.rb
|
185
|
+
- lib/bel_parser/parsers/expression/statement_nested.rl
|
186
|
+
- lib/bel_parser/parsers/expression/statement_observed_term.rb
|
187
|
+
- lib/bel_parser/parsers/expression/statement_observed_term.rl
|
188
|
+
- lib/bel_parser/parsers/expression/statement_simple.rb
|
189
|
+
- lib/bel_parser/parsers/expression/statement_simple.rl
|
190
|
+
- lib/bel_parser/parsers/expression/term.rb
|
191
|
+
- lib/bel_parser/parsers/expression/term.rl
|
192
|
+
- lib/bel_parser/parsers/line_parser.rb
|
193
|
+
- lib/bel_parser/parsers/mixin/buffer.rb
|
194
|
+
- lib/bel_parser/parsers/nonblocking_io_wrapper.rb
|
195
|
+
- lib/bel_parser/script/parser.rb
|
196
|
+
- lib/bel_parser/vendor/ast.rb
|
197
|
+
- lib/bel_parser/vendor/ast/node.rb
|
198
|
+
- lib/bel_parser/vendor/ast/processor.rb
|
199
|
+
- lib/bel_parser/vendor/ast/processor/mixin.rb
|
200
|
+
- lib/bel_parser/vendor/ast/sexp.rb
|
201
|
+
homepage: https://github.com/OpenBEL/bel_parser
|
202
|
+
licenses:
|
203
|
+
- Apache-2.0
|
204
|
+
metadata: {}
|
205
|
+
post_install_message:
|
206
|
+
rdoc_options: []
|
207
|
+
require_paths:
|
208
|
+
- lib
|
209
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
210
|
+
requirements:
|
211
|
+
- - ">="
|
212
|
+
- !ruby/object:Gem::Version
|
213
|
+
version: 2.0.0
|
214
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
215
|
+
requirements:
|
216
|
+
- - ">"
|
217
|
+
- !ruby/object:Gem::Version
|
218
|
+
version: 1.3.1
|
219
|
+
requirements: []
|
220
|
+
rubyforge_project:
|
221
|
+
rubygems_version: 2.5.1
|
222
|
+
signing_key:
|
223
|
+
specification_version: 4
|
224
|
+
summary: Parser for Biolgical Expression Language.
|
225
|
+
test_files: []
|
226
|
+
has_rdoc:
|