personify 1.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/.gitignore +1 -0
- data/LICENSE +20 -0
- data/README.md +172 -0
- data/Rakefile +53 -0
- data/VERSION +1 -0
- data/doc/syntax_ideas.md +141 -0
- data/lib/personify/context.rb +55 -0
- data/lib/personify/parser/personify.rb +1071 -0
- data/lib/personify/parser/personify.treetop +107 -0
- data/lib/personify/parser/personify_node_classes.rb +121 -0
- data/lib/personify/template.rb +17 -0
- data/lib/personify.rb +8 -0
- data/script/generate_parser.rb +6 -0
- data/test/context_test.rb +122 -0
- data/test/fixtures/multiple_tags.txt +8 -0
- data/test/parse_runner.rb +60 -0
- data/test/parser_test.rb +291 -0
- data/test/test_helper.rb +16 -0
- data/vendor/treetop/.gitignore +5 -0
- data/vendor/treetop/History.txt +9 -0
- data/vendor/treetop/README +164 -0
- data/vendor/treetop/Rakefile +20 -0
- data/vendor/treetop/Treetop.tmbundle/Snippets/grammar ___ end.tmSnippet +20 -0
- data/vendor/treetop/Treetop.tmbundle/Snippets/rule ___ end.tmSnippet +18 -0
- data/vendor/treetop/Treetop.tmbundle/Syntaxes/Treetop Grammar.tmLanguage +251 -0
- data/vendor/treetop/Treetop.tmbundle/info.plist +10 -0
- data/vendor/treetop/bin/tt +28 -0
- data/vendor/treetop/doc/contributing_and_planned_features.markdown +103 -0
- data/vendor/treetop/doc/grammar_composition.markdown +65 -0
- data/vendor/treetop/doc/index.markdown +90 -0
- data/vendor/treetop/doc/pitfalls_and_advanced_techniques.markdown +51 -0
- data/vendor/treetop/doc/semantic_interpretation.markdown +189 -0
- data/vendor/treetop/doc/site.rb +110 -0
- data/vendor/treetop/doc/sitegen.rb +60 -0
- data/vendor/treetop/doc/syntactic_recognition.markdown +100 -0
- data/vendor/treetop/doc/using_in_ruby.markdown +21 -0
- data/vendor/treetop/examples/lambda_calculus/arithmetic.rb +551 -0
- data/vendor/treetop/examples/lambda_calculus/arithmetic.treetop +97 -0
- data/vendor/treetop/examples/lambda_calculus/arithmetic_node_classes.rb +7 -0
- data/vendor/treetop/examples/lambda_calculus/arithmetic_test.rb +54 -0
- data/vendor/treetop/examples/lambda_calculus/lambda_calculus +0 -0
- data/vendor/treetop/examples/lambda_calculus/lambda_calculus.rb +718 -0
- data/vendor/treetop/examples/lambda_calculus/lambda_calculus.treetop +132 -0
- data/vendor/treetop/examples/lambda_calculus/lambda_calculus_node_classes.rb +5 -0
- data/vendor/treetop/examples/lambda_calculus/lambda_calculus_test.rb +89 -0
- data/vendor/treetop/examples/lambda_calculus/test_helper.rb +18 -0
- data/vendor/treetop/lib/treetop/bootstrap_gen_1_metagrammar.rb +45 -0
- data/vendor/treetop/lib/treetop/compiler/grammar_compiler.rb +40 -0
- data/vendor/treetop/lib/treetop/compiler/lexical_address_space.rb +17 -0
- data/vendor/treetop/lib/treetop/compiler/metagrammar.rb +2955 -0
- data/vendor/treetop/lib/treetop/compiler/metagrammar.treetop +404 -0
- data/vendor/treetop/lib/treetop/compiler/node_classes/anything_symbol.rb +20 -0
- data/vendor/treetop/lib/treetop/compiler/node_classes/atomic_expression.rb +14 -0
- data/vendor/treetop/lib/treetop/compiler/node_classes/character_class.rb +22 -0
- data/vendor/treetop/lib/treetop/compiler/node_classes/choice.rb +31 -0
- data/vendor/treetop/lib/treetop/compiler/node_classes/declaration_sequence.rb +24 -0
- data/vendor/treetop/lib/treetop/compiler/node_classes/grammar.rb +28 -0
- data/vendor/treetop/lib/treetop/compiler/node_classes/inline_module.rb +27 -0
- data/vendor/treetop/lib/treetop/compiler/node_classes/nonterminal.rb +13 -0
- data/vendor/treetop/lib/treetop/compiler/node_classes/optional.rb +19 -0
- data/vendor/treetop/lib/treetop/compiler/node_classes/parenthesized_expression.rb +9 -0
- data/vendor/treetop/lib/treetop/compiler/node_classes/parsing_expression.rb +138 -0
- data/vendor/treetop/lib/treetop/compiler/node_classes/parsing_rule.rb +55 -0
- data/vendor/treetop/lib/treetop/compiler/node_classes/predicate.rb +45 -0
- data/vendor/treetop/lib/treetop/compiler/node_classes/repetition.rb +55 -0
- data/vendor/treetop/lib/treetop/compiler/node_classes/sequence.rb +68 -0
- data/vendor/treetop/lib/treetop/compiler/node_classes/terminal.rb +20 -0
- data/vendor/treetop/lib/treetop/compiler/node_classes/transient_prefix.rb +9 -0
- data/vendor/treetop/lib/treetop/compiler/node_classes/treetop_file.rb +9 -0
- data/vendor/treetop/lib/treetop/compiler/node_classes.rb +19 -0
- data/vendor/treetop/lib/treetop/compiler/ruby_builder.rb +113 -0
- data/vendor/treetop/lib/treetop/compiler.rb +6 -0
- data/vendor/treetop/lib/treetop/ruby_extensions/string.rb +42 -0
- data/vendor/treetop/lib/treetop/ruby_extensions.rb +2 -0
- data/vendor/treetop/lib/treetop/runtime/compiled_parser.rb +95 -0
- data/vendor/treetop/lib/treetop/runtime/interval_skip_list/head_node.rb +15 -0
- data/vendor/treetop/lib/treetop/runtime/interval_skip_list/interval_skip_list.rb +200 -0
- data/vendor/treetop/lib/treetop/runtime/interval_skip_list/node.rb +164 -0
- data/vendor/treetop/lib/treetop/runtime/interval_skip_list.rb +4 -0
- data/vendor/treetop/lib/treetop/runtime/syntax_node.rb +72 -0
- data/vendor/treetop/lib/treetop/runtime/terminal_parse_failure.rb +16 -0
- data/vendor/treetop/lib/treetop/runtime/terminal_syntax_node.rb +17 -0
- data/vendor/treetop/lib/treetop/runtime.rb +5 -0
- data/vendor/treetop/lib/treetop/version.rb +9 -0
- data/vendor/treetop/lib/treetop.rb +11 -0
- data/vendor/treetop/script/generate_metagrammar.rb +14 -0
- data/vendor/treetop/script/svnadd +11 -0
- data/vendor/treetop/script/svnrm +11 -0
- data/vendor/treetop/spec/compiler/and_predicate_spec.rb +36 -0
- data/vendor/treetop/spec/compiler/anything_symbol_spec.rb +52 -0
- data/vendor/treetop/spec/compiler/character_class_spec.rb +188 -0
- data/vendor/treetop/spec/compiler/choice_spec.rb +80 -0
- data/vendor/treetop/spec/compiler/circular_compilation_spec.rb +28 -0
- data/vendor/treetop/spec/compiler/failure_propagation_functional_spec.rb +21 -0
- data/vendor/treetop/spec/compiler/grammar_compiler_spec.rb +84 -0
- data/vendor/treetop/spec/compiler/grammar_spec.rb +41 -0
- data/vendor/treetop/spec/compiler/nonterminal_symbol_spec.rb +40 -0
- data/vendor/treetop/spec/compiler/not_predicate_spec.rb +38 -0
- data/vendor/treetop/spec/compiler/one_or_more_spec.rb +35 -0
- data/vendor/treetop/spec/compiler/optional_spec.rb +37 -0
- data/vendor/treetop/spec/compiler/parenthesized_expression_spec.rb +19 -0
- data/vendor/treetop/spec/compiler/parsing_rule_spec.rb +32 -0
- data/vendor/treetop/spec/compiler/sequence_spec.rb +115 -0
- data/vendor/treetop/spec/compiler/terminal_spec.rb +81 -0
- data/vendor/treetop/spec/compiler/terminal_symbol_spec.rb +37 -0
- data/vendor/treetop/spec/compiler/test_grammar.treetop +7 -0
- data/vendor/treetop/spec/compiler/test_grammar.tt +7 -0
- data/vendor/treetop/spec/compiler/test_grammar_do.treetop +7 -0
- data/vendor/treetop/spec/compiler/zero_or_more_spec.rb +56 -0
- data/vendor/treetop/spec/composition/a.treetop +11 -0
- data/vendor/treetop/spec/composition/b.treetop +11 -0
- data/vendor/treetop/spec/composition/c.treetop +10 -0
- data/vendor/treetop/spec/composition/d.treetop +10 -0
- data/vendor/treetop/spec/composition/grammar_composition_spec.rb +26 -0
- data/vendor/treetop/spec/ruby_extensions/string_spec.rb +32 -0
- data/vendor/treetop/spec/runtime/compiled_parser_spec.rb +101 -0
- data/vendor/treetop/spec/runtime/interval_skip_list/delete_spec.rb +147 -0
- data/vendor/treetop/spec/runtime/interval_skip_list/expire_range_spec.rb +349 -0
- data/vendor/treetop/spec/runtime/interval_skip_list/insert_and_delete_node.rb +385 -0
- data/vendor/treetop/spec/runtime/interval_skip_list/insert_spec.rb +660 -0
- data/vendor/treetop/spec/runtime/interval_skip_list/interval_skip_list_spec.graffle +6175 -0
- data/vendor/treetop/spec/runtime/interval_skip_list/interval_skip_list_spec.rb +58 -0
- data/vendor/treetop/spec/runtime/interval_skip_list/palindromic_fixture.rb +23 -0
- data/vendor/treetop/spec/runtime/interval_skip_list/palindromic_fixture_spec.rb +164 -0
- data/vendor/treetop/spec/runtime/interval_skip_list/spec_helper.rb +84 -0
- data/vendor/treetop/spec/runtime/syntax_node_spec.rb +53 -0
- data/vendor/treetop/spec/spec_helper.rb +106 -0
- data/vendor/treetop/spec/spec_suite.rb +4 -0
- data/vendor/treetop/treetop.gemspec +18 -0
- metadata +196 -0
|
@@ -0,0 +1,107 @@
|
|
|
1
|
+
grammar PersonifyLanguage
|
|
2
|
+
|
|
3
|
+
##################
|
|
4
|
+
# Template parts
|
|
5
|
+
|
|
6
|
+
rule template
|
|
7
|
+
(parts_including_tail)* <Template>
|
|
8
|
+
end
|
|
9
|
+
|
|
10
|
+
rule parts_including_tail
|
|
11
|
+
part / tail_part
|
|
12
|
+
end
|
|
13
|
+
|
|
14
|
+
rule part
|
|
15
|
+
text / substitutable
|
|
16
|
+
end
|
|
17
|
+
|
|
18
|
+
rule tail_part
|
|
19
|
+
'[' part <TailPart>
|
|
20
|
+
end
|
|
21
|
+
|
|
22
|
+
|
|
23
|
+
##################
|
|
24
|
+
# Substitutable
|
|
25
|
+
|
|
26
|
+
rule substitutable
|
|
27
|
+
'[' space expressions space ']' <Substitutable>
|
|
28
|
+
end
|
|
29
|
+
|
|
30
|
+
##################
|
|
31
|
+
# Blocks
|
|
32
|
+
|
|
33
|
+
rule block
|
|
34
|
+
'DO' space ']' block_content '[' space 'END' <Block>
|
|
35
|
+
end
|
|
36
|
+
|
|
37
|
+
rule block_content
|
|
38
|
+
(part)* <Literal>
|
|
39
|
+
end
|
|
40
|
+
|
|
41
|
+
##################
|
|
42
|
+
# Expressions
|
|
43
|
+
|
|
44
|
+
rule expressions
|
|
45
|
+
expression alternatives:(space "|" space expression_or_string)* <Expressions>
|
|
46
|
+
end
|
|
47
|
+
|
|
48
|
+
rule expression
|
|
49
|
+
function / logical / key / string
|
|
50
|
+
end
|
|
51
|
+
|
|
52
|
+
rule expression_or_string
|
|
53
|
+
expression / implicit_string
|
|
54
|
+
end
|
|
55
|
+
|
|
56
|
+
##################
|
|
57
|
+
# Functions
|
|
58
|
+
|
|
59
|
+
rule function
|
|
60
|
+
key space "(" space parameters space ")" space block:(block)? <Function>
|
|
61
|
+
end
|
|
62
|
+
|
|
63
|
+
rule parameters
|
|
64
|
+
first_param:(expression_or_string)? more_expressions:(space "," space expression_or_string)* <Parameter>
|
|
65
|
+
end
|
|
66
|
+
|
|
67
|
+
##################
|
|
68
|
+
# Logical
|
|
69
|
+
|
|
70
|
+
rule logical
|
|
71
|
+
first_expression:(function / key / string) space operator:("||" / "&&") space next_expression:(expression) <Logical>
|
|
72
|
+
end
|
|
73
|
+
|
|
74
|
+
##################
|
|
75
|
+
# Keys
|
|
76
|
+
|
|
77
|
+
rule key
|
|
78
|
+
!reserved ([A-Z0-9] [A-Z0-9._]*) <Key>
|
|
79
|
+
end
|
|
80
|
+
|
|
81
|
+
rule reserved
|
|
82
|
+
"END" / "DO"
|
|
83
|
+
end
|
|
84
|
+
|
|
85
|
+
##################
|
|
86
|
+
# Literals
|
|
87
|
+
|
|
88
|
+
rule string
|
|
89
|
+
'"' string_value '"' <PString>
|
|
90
|
+
end
|
|
91
|
+
|
|
92
|
+
rule implicit_string
|
|
93
|
+
[^|\],)]+ <Literal>
|
|
94
|
+
end
|
|
95
|
+
|
|
96
|
+
rule string_value
|
|
97
|
+
[^"]* <Literal>
|
|
98
|
+
end
|
|
99
|
+
|
|
100
|
+
rule text
|
|
101
|
+
[^\[] [^\[]* <Literal>
|
|
102
|
+
end
|
|
103
|
+
|
|
104
|
+
rule space
|
|
105
|
+
[ \n]*
|
|
106
|
+
end
|
|
107
|
+
end
|
|
@@ -0,0 +1,121 @@
|
|
|
1
|
+
module PersonifyLanguage
|
|
2
|
+
class Template < Treetop::Runtime::SyntaxNode
|
|
3
|
+
def eval(env={})
|
|
4
|
+
elements.map{|e| e.eval(env) }.join("")
|
|
5
|
+
end
|
|
6
|
+
end
|
|
7
|
+
|
|
8
|
+
class TailPart < Treetop::Runtime::SyntaxNode
|
|
9
|
+
def eval(env)
|
|
10
|
+
"[" + part.eval(env)
|
|
11
|
+
end
|
|
12
|
+
end
|
|
13
|
+
|
|
14
|
+
class Substitutable < Treetop::Runtime::SyntaxNode
|
|
15
|
+
def eval(env)
|
|
16
|
+
# puts expressions.inspect
|
|
17
|
+
last_eval = expressions.eval(env)
|
|
18
|
+
if last_eval.nil?
|
|
19
|
+
text_value
|
|
20
|
+
else
|
|
21
|
+
last_eval
|
|
22
|
+
end
|
|
23
|
+
end
|
|
24
|
+
end
|
|
25
|
+
|
|
26
|
+
class Expressions < Treetop::Runtime::SyntaxNode
|
|
27
|
+
def eval(env)
|
|
28
|
+
last_value = nil
|
|
29
|
+
expressions.detect do |exp|
|
|
30
|
+
last_value = exp.eval(env)
|
|
31
|
+
end
|
|
32
|
+
last_value
|
|
33
|
+
end
|
|
34
|
+
|
|
35
|
+
def expressions
|
|
36
|
+
[expression] + alternatives.elements.map {|elt| elt.expression_or_string}
|
|
37
|
+
end
|
|
38
|
+
end
|
|
39
|
+
|
|
40
|
+
class Key < Treetop::Runtime::SyntaxNode
|
|
41
|
+
def eval(env)
|
|
42
|
+
keys = self.to_s.split(".")
|
|
43
|
+
keys.inject(env){|acc,k| acc && acc[k] }
|
|
44
|
+
end
|
|
45
|
+
|
|
46
|
+
def name
|
|
47
|
+
text_value
|
|
48
|
+
end
|
|
49
|
+
|
|
50
|
+
def to_s
|
|
51
|
+
self.name.downcase.to_s
|
|
52
|
+
end
|
|
53
|
+
end
|
|
54
|
+
|
|
55
|
+
class PString < Treetop::Runtime::SyntaxNode
|
|
56
|
+
def eval(env={})
|
|
57
|
+
string_value.eval(env)
|
|
58
|
+
end
|
|
59
|
+
end
|
|
60
|
+
|
|
61
|
+
class Literal < Treetop::Runtime::SyntaxNode
|
|
62
|
+
def eval(env={})
|
|
63
|
+
text_value
|
|
64
|
+
end
|
|
65
|
+
|
|
66
|
+
def to_s
|
|
67
|
+
text_value
|
|
68
|
+
end
|
|
69
|
+
end
|
|
70
|
+
|
|
71
|
+
class Logical < Treetop::Runtime::SyntaxNode
|
|
72
|
+
def eval(env)
|
|
73
|
+
fe = first_expression.eval(env)
|
|
74
|
+
ne = next_expression.eval(env)
|
|
75
|
+
|
|
76
|
+
case operator.text_value
|
|
77
|
+
when "&&" : fe && ne
|
|
78
|
+
when "||" : fe || ne
|
|
79
|
+
end
|
|
80
|
+
end
|
|
81
|
+
end
|
|
82
|
+
|
|
83
|
+
class Function < Treetop::Runtime::SyntaxNode
|
|
84
|
+
def eval(env={})
|
|
85
|
+
if env.respond_to?(:allow_method?) && env.allow_method?(key.to_s)
|
|
86
|
+
fn = env.method(key.to_s)
|
|
87
|
+
else
|
|
88
|
+
fn = self.key.eval(env)
|
|
89
|
+
end
|
|
90
|
+
|
|
91
|
+
if fn && fn.respond_to?(:call)
|
|
92
|
+
fn.call(*self.function_parameters(env))
|
|
93
|
+
else
|
|
94
|
+
fn
|
|
95
|
+
end
|
|
96
|
+
rescue StandardError
|
|
97
|
+
return nil
|
|
98
|
+
end
|
|
99
|
+
|
|
100
|
+
def function_parameters(env)
|
|
101
|
+
values = parameters.eval(env)
|
|
102
|
+
values << self.block.eval(env) if self.block.kind_of?(Block)
|
|
103
|
+
values
|
|
104
|
+
end
|
|
105
|
+
end
|
|
106
|
+
|
|
107
|
+
class Parameter < Treetop::Runtime::SyntaxNode
|
|
108
|
+
def eval(env={})
|
|
109
|
+
self.parameters.map{|param| param.eval(env) }
|
|
110
|
+
end
|
|
111
|
+
def parameters
|
|
112
|
+
(self.first_param.respond_to?(:eval) ? [first_param] : []) + more_expressions.elements.map {|elt| elt.expression_or_string}
|
|
113
|
+
end
|
|
114
|
+
end
|
|
115
|
+
|
|
116
|
+
class Block < Treetop::Runtime::SyntaxNode
|
|
117
|
+
def eval(env={})
|
|
118
|
+
block_content.to_s
|
|
119
|
+
end
|
|
120
|
+
end
|
|
121
|
+
end
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
module Personify
|
|
2
|
+
class Template
|
|
3
|
+
|
|
4
|
+
attr_reader :template
|
|
5
|
+
|
|
6
|
+
def initialize(template)
|
|
7
|
+
parser = PersonifyLanguageParser.new
|
|
8
|
+
@template = parser.parse(template)
|
|
9
|
+
end
|
|
10
|
+
|
|
11
|
+
def render(local_assigns={}, context = DefaultContext.new)
|
|
12
|
+
context.local_assigns = local_assigns
|
|
13
|
+
@template.eval(context)
|
|
14
|
+
end
|
|
15
|
+
|
|
16
|
+
end
|
|
17
|
+
end
|
data/lib/personify.rb
ADDED
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
require File.dirname(__FILE__) + '/../vendor/treetop/lib/treetop'
|
|
2
|
+
require File.dirname(__FILE__) + '/personify/parser/personify_node_classes'
|
|
3
|
+
require File.dirname(__FILE__) + '/personify/parser/personify'
|
|
4
|
+
require File.dirname(__FILE__) + '/personify/template'
|
|
5
|
+
require File.dirname(__FILE__) + '/personify/context'
|
|
6
|
+
|
|
7
|
+
module Personify
|
|
8
|
+
end
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
require File.dirname(__FILE__) + "/../vendor/treetop/lib/treetop"
|
|
2
|
+
|
|
3
|
+
target_dir = File.dirname(__FILE__) + "/../lib/parser/"
|
|
4
|
+
|
|
5
|
+
compiler = Treetop::Compiler::GrammarCompiler.new
|
|
6
|
+
compiler.compile(File.join(target_dir,"personify.treetop"), File.join(target_dir,"personify.rb"))
|
|
@@ -0,0 +1,122 @@
|
|
|
1
|
+
require 'test_helper'
|
|
2
|
+
require File.dirname(__FILE__) + "/../vendor/treetop/lib/treetop"
|
|
3
|
+
Treetop.load "../lib/personify/parser/personify"
|
|
4
|
+
# require File.dirname(__FILE__) + "/../personify"
|
|
5
|
+
|
|
6
|
+
class ContextTest < Test::Unit::TestCase
|
|
7
|
+
include ParserTestHelper
|
|
8
|
+
|
|
9
|
+
context "A Context class" do
|
|
10
|
+
setup do
|
|
11
|
+
@parser = PersonifyLanguageParser.new
|
|
12
|
+
|
|
13
|
+
@c = Class.new(Personify::Context) do
|
|
14
|
+
def true
|
|
15
|
+
"true"
|
|
16
|
+
end
|
|
17
|
+
context_method :true
|
|
18
|
+
|
|
19
|
+
def block(blk)
|
|
20
|
+
"|#{blk}|"
|
|
21
|
+
end
|
|
22
|
+
context_method :block
|
|
23
|
+
|
|
24
|
+
def do_not_call
|
|
25
|
+
raise
|
|
26
|
+
end
|
|
27
|
+
|
|
28
|
+
end
|
|
29
|
+
end
|
|
30
|
+
|
|
31
|
+
|
|
32
|
+
should "allow setting of local_assigns" do
|
|
33
|
+
@i = @c.new
|
|
34
|
+
assert @i.local_assigns = {:key => "value"}
|
|
35
|
+
assert @i.has_key?(:key)
|
|
36
|
+
assert_equal "value", @i[:key]
|
|
37
|
+
end
|
|
38
|
+
|
|
39
|
+
should "allow calling of a context method" do
|
|
40
|
+
assert_equal "true", parse("[TRUE()]").eval(@c.new)
|
|
41
|
+
end
|
|
42
|
+
|
|
43
|
+
should "not allow calling of non context methods" do
|
|
44
|
+
assert_equal "[DO_NOT_CALL()]", parse("[DO_NOT_CALL()]").eval(@c.new)
|
|
45
|
+
end
|
|
46
|
+
|
|
47
|
+
should "allow calling of a context method with block" do
|
|
48
|
+
assert_equal "|out|", parse("[BLOCK(\"out\")]").eval(@c.new)
|
|
49
|
+
assert_equal "|out|", parse("[BLOCK() DO]out[END]").eval(@c.new)
|
|
50
|
+
end
|
|
51
|
+
|
|
52
|
+
end
|
|
53
|
+
|
|
54
|
+
|
|
55
|
+
context "The DefaultContext" do
|
|
56
|
+
setup do
|
|
57
|
+
@context = Personify::DefaultContext.new
|
|
58
|
+
@context.local_assigns = {"value" => "value", "empty" => "", "nil" => nil, "false" => false}
|
|
59
|
+
|
|
60
|
+
@parser = PersonifyLanguageParser.new
|
|
61
|
+
end
|
|
62
|
+
|
|
63
|
+
context "if method" do
|
|
64
|
+
|
|
65
|
+
should "accept 2 arguments" do
|
|
66
|
+
assert_equal "out", parse("[IF(\"true\", \"out\")]").eval(@context)
|
|
67
|
+
assert_equal "out", parse("[IF(\"true\") DO]out[END]").eval(@context)
|
|
68
|
+
end
|
|
69
|
+
|
|
70
|
+
should "not raise exception with 1 argument" do
|
|
71
|
+
assert_nothing_raised do
|
|
72
|
+
parse("[IF(\"out\")]").eval(@context)
|
|
73
|
+
end
|
|
74
|
+
end
|
|
75
|
+
|
|
76
|
+
should "not raise exception with more than 2 arguments" do
|
|
77
|
+
assert_nothing_raised do
|
|
78
|
+
parse("[IF(\"out\", \"2\", \"3\")]").eval(@context)
|
|
79
|
+
end
|
|
80
|
+
end
|
|
81
|
+
|
|
82
|
+
should "return nil with one argument" do
|
|
83
|
+
assert_equal "[IF(\"out\")]", parse("[IF(\"out\")]").eval(@context)
|
|
84
|
+
end
|
|
85
|
+
|
|
86
|
+
should "return nil with more than 2 arguments" do
|
|
87
|
+
assert_equal "[IF(\"out\", \"2\", \"3\")]", parse("[IF(\"out\", \"2\", \"3\")]").eval(@context)
|
|
88
|
+
assert_equal "test", parse("[IF(\"out\", \"2\", \"3\") | \"test\"]").eval(@context)
|
|
89
|
+
assert_equal "[IF(\"out\",\"2\") DO]3[END]", parse("[IF(\"out\",\"2\") DO]3[END]").eval(@context)
|
|
90
|
+
end
|
|
91
|
+
|
|
92
|
+
should "return second argument if first argument is not empty" do
|
|
93
|
+
assert_equal "out", parse("[IF(\"true\", \"out\")]").eval(@context)
|
|
94
|
+
assert_equal "out", parse("[IF(\"true\") DO]out[END]").eval(@context)
|
|
95
|
+
assert_equal "out", parse("[IF(VALUE) DO]out[END]").eval(@context)
|
|
96
|
+
assert_equal "out", parse("[IF(EMPTY) DO]out[END]").eval(@context)
|
|
97
|
+
end
|
|
98
|
+
|
|
99
|
+
should "return nil if first argument is false" do
|
|
100
|
+
assert_equal "[IF(NIL) DO]out[END]", parse("[IF(NIL) DO]out[END]").eval(@context)
|
|
101
|
+
assert_equal "[IF(FALSE) DO]out[END]", parse("[IF(FALSE) DO]out[END]").eval(@context)
|
|
102
|
+
assert_equal "", parse("[IF(FALSE) DO]out[END | \"\"]").eval(@context)
|
|
103
|
+
end
|
|
104
|
+
|
|
105
|
+
should "be able to do if-elsif-else with | operator" do
|
|
106
|
+
templ = "[IF(THIS) DO]this[END | IF(THAT) DO]that[END | \"other\"]"
|
|
107
|
+
|
|
108
|
+
@context.local_assigns = {"this" => true, "that" => true}
|
|
109
|
+
assert_equal "this", parse(templ).eval(@context)
|
|
110
|
+
|
|
111
|
+
@context.local_assigns = {"this" => false, "that" => true}
|
|
112
|
+
assert_equal "that", parse(templ).eval(@context)
|
|
113
|
+
|
|
114
|
+
@context.local_assigns = {"this" => false, "that" => false}
|
|
115
|
+
assert_equal "other", parse(templ).eval(@context)
|
|
116
|
+
end
|
|
117
|
+
|
|
118
|
+
end
|
|
119
|
+
|
|
120
|
+
end
|
|
121
|
+
|
|
122
|
+
end
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
Lorem ipsum dolor sit amet, consectetuer adipiscing elit. In tristique sapien in tellus dapibus tincidunt. Ut eu tortor vitae quam eleifend lacinia. Nulla facilisi. Vivamus purus. Integer ultrices commodo pede. Sed rutrum, sapien vel tempor porta, turpis justo malesuada nisl, quis vestibulum leo augue laoreet [TAG3] lacus. Pellentesque mollis, ante ut sodales ullamcorper, erat tellus facilisis ipsum, et ornare urna nunc quis est. Etiam nibh elit, consectetuer nec, pretium eu, rutrum sed, arcu. Nunc sit amet metus nec orci consequat semper. Nullam iaculis felis vitae metus. Pellentesque molestie. Sed quis nisi ut eros placerat posuere. Aenean pellentesque nunc non arcu. Vivamus mauris. Nulla lorem. Curabitur tellus nisl, porta eget, vehicula eu, consequat id, tortor. Sed vitae est. Donec id diam eu tellus faucibus commodo. Aliquam vel turpis vel urna blandit malesuada. Nulla dui. "[TAG1]"
|
|
2
|
+
|
|
3
|
+
Cras sit amet ante sit amet elit tincidunt fermentum. Vivamus hendrerit, nulla non elementum rutrum, nulla libero auctor nunc, vulputate malesuada nisi sapien non velit. Nam eu felis. Vivamus ac mi et ipsum egestas congue. Pellentesque eu odio vitae diam molestie venenatis. Sed eleifend nulla ut ipsum. Nam tincidunt ultrices neque. Duis erat. Sed ipsum arcu, laoreet eget, rutrum quis, accumsan at, arcu. Pellentesque eu ipsum a felis pulvinar laoreet.[TAG4] Sed viverra est vitae turpis. Maecenas tortor metus, aliquet et, ullamcorper auctor, mattis quis, leo. Lorem ipsum dolor sit amet, consectetuer adipiscing elit. [TAG3] Mauris ut eros sed ipsum facilisis nonummy. Fusce pretium, magna vestibulum semper nonummy, purus eros scelerisque mi, id luctus est nulla eget ante. Suspendisse quis nibh. Nullam pharetra tristique augue. Integer porta luctus ligula. Sed tincidunt.
|
|
4
|
+
|
|
5
|
+
Fusce varius commodo lacus. Nullam tincidunt [TAG2] massa lobortis sapien. Sed turpis erat, hendrerit nec, fringilla eu, congue semper, tortor. Ut eget tellus sed mauris pellentesque imperdiet. Sed elementum dictum ipsum. Ut pharetra. Sed eleifend cras amet.
|
|
6
|
+
[TAG1][TAG1][TAG1][TAG1][TAG1][TAG1][TAG1][TAG1][TAG1][TAG1][TAG1][TAG1]
|
|
7
|
+
|
|
8
|
+
[NONEXISTANT][NONEXISTANT][NONEXISTANT][NONEXISTANT][NONEXISTANT][NONEXISTANT]
|
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
require File.dirname(__FILE__) + "/../vendor/treetop/lib/treetop"
|
|
2
|
+
|
|
3
|
+
require '../lib/parser/personify_node_classes'
|
|
4
|
+
require '../lib/context'
|
|
5
|
+
Treetop.load "../lib/parser/personify"
|
|
6
|
+
|
|
7
|
+
# STR = "a [TAG] is never [TAG] with [TAG2] and [troepie] bla"
|
|
8
|
+
# STR = "[A1|B1|\"str\"] [A1|B1|str] [AB(T1)]"
|
|
9
|
+
# STR = "[AND(\"1\", 2,3)] <a href=\"http://mlr1.nl/r/[TOKEN]/539\"> financiële"
|
|
10
|
+
# STR = "[FUN() DO]\nvalue\n[T1]\n[END | \"bla\"] bla [FUN() DO]OER![END | '']"
|
|
11
|
+
# STR = "[IF(DETECT(FALSE,FALSE, TRUE)) DO]yes[END | \"\"]"
|
|
12
|
+
STR = "[A || B]"
|
|
13
|
+
# STR = "[TRUE DO]bla[END]"
|
|
14
|
+
# STR = "[FUN()]"
|
|
15
|
+
@parser = PersonifyLanguageParser.new
|
|
16
|
+
|
|
17
|
+
class TheContext < Personify::DefaultContext
|
|
18
|
+
|
|
19
|
+
context_method :if
|
|
20
|
+
|
|
21
|
+
def detect(*statements)
|
|
22
|
+
statements.detect{|s| s }
|
|
23
|
+
end
|
|
24
|
+
context_method :detect
|
|
25
|
+
end
|
|
26
|
+
|
|
27
|
+
def clean_tree(root_node)
|
|
28
|
+
return if(root_node.elements.nil?)
|
|
29
|
+
root_node.elements.delete_if{|node| node.class.name == "Treetop::Runtime::SyntaxNode" }
|
|
30
|
+
root_node.elements.each {|node| clean_tree(node) }
|
|
31
|
+
end
|
|
32
|
+
|
|
33
|
+
if result = @parser.parse(STR)
|
|
34
|
+
puts "done"
|
|
35
|
+
puts result
|
|
36
|
+
puts "=" * 50
|
|
37
|
+
# clean_tree(result)
|
|
38
|
+
puts result.inspect
|
|
39
|
+
puts "=" * 50
|
|
40
|
+
|
|
41
|
+
t = TheContext.new
|
|
42
|
+
t.local_assigns = {
|
|
43
|
+
"a" => "lA",
|
|
44
|
+
"b" => "lB",
|
|
45
|
+
"fun" => Proc.new{|p| "--#{p}--" },
|
|
46
|
+
"true" => true,
|
|
47
|
+
"false" => false,
|
|
48
|
+
"t1" => "tt",
|
|
49
|
+
"tag" => "henk",
|
|
50
|
+
"ab" => Proc.new{|p| p.inspect },
|
|
51
|
+
"and" => Proc.new{|*c| c.join(" & ") }
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
puts result.eval(t).inspect
|
|
55
|
+
else
|
|
56
|
+
puts "FAIL"
|
|
57
|
+
puts @parser.inspect
|
|
58
|
+
puts "=" * 50
|
|
59
|
+
puts @parser.terminal_failures.join("\n")
|
|
60
|
+
end
|