Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

WIP:still broken, but less drastically, and for logic rather than stu…

…pidity reasons now
  • Loading branch information...
commit ea8b0ccddec9d19e038fd8ed5b8c1d54ef01cd66 1 parent f70ff4e
Brian Glusman authored
Showing with 13 additions and 10 deletions.
  1. +1 −0  lib/outlaw.rb
  2. +8 −6 lib/outlaw/law_parser.rb
  3. +4 −4 lib/outlaw/rule.rb
View
1  lib/outlaw.rb
@@ -1,4 +1,5 @@
require 'ripper'
+require 'pry'
require_relative 'outlaw/law_parser'
require_relative 'outlaw/enforcement'
require_relative 'outlaw/rule'
View
14 lib/outlaw/law_parser.rb
@@ -1,13 +1,14 @@
module Outlaw
module LawParser
extend self
- def parse(restriction, rule)
+ def parse(rule)
+ restriction = rule.pattern
tokens = restriction.split
parsed_restriction = []
tokens.each do |token|
case
when special_case?(string_to_sym(token))
- handle_special(token, rule, parsed_restriction)
+ handle_special(string_to_sym(token), rule, parsed_restriction)
when multipart?(token) #this handles multi-token literals, Const.new etc
parsed_restriction += Ripper.lex(token)
.reduce([]){|array, tkn|
@@ -29,11 +30,12 @@ def handle_special(token, rule, parsed_restriction)
case token
when *RULE_CASES
rule.modifications ||= []
- rule.modifications << string_to_sym(token)
+ rule.modifications << token
when *FUNCTION_CASES
- parsed_restriction << lambda ->(*args) do
- rule.send(token, *args) #actually required to take 3 arguments
- end
+ parsed_restriction << ->(*args) {
+ target = args.shift
+ target.send(token, *args) #actually required to take self + 3 arguments
+ }
end
end
View
8 lib/outlaw/rule.rb
@@ -9,7 +9,7 @@ def initialize(pattern, message=nil, &detection_block)
end
def violation?(code)
- @detection_block = LawParser.parse(pattern, self) if detection_block.nil?
+ @detection_block = LawParser.parse(self) if detection_block.nil?
detect_violation(code)
end
@@ -55,11 +55,11 @@ def test(program, start_index, pattern)
code = program[index]
part = pattern[pattern_index]
return false if code.nil?
- (index += 1 && next) if Outlaw.ignore_types.include?(token_type(code))
if part.kind_of?(Proc)
- return false unless part.call(pattern[pattern_index+1], program, index)
- next #^require all function cases must take these 3 arguments
+ return false unless part.call(self, pattern[pattern_index+1], program, index)
+ next #^require all function cases must take these 4 arguments
end
+ (index += 1 && next) if Outlaw.ignore_types.include?(token_type(code))
return false unless match_token?(code, part, params[part])
pattern_index +=1
return true if pattern_index >= pattern.length
Please sign in to comment.
Something went wrong with that request. Please try again.