Skip to content

Commit

Permalink
Start implementing the parser
Browse files Browse the repository at this point in the history
  • Loading branch information
docelic committed Jan 7, 2018
1 parent 5f9b6f5 commit e3ea183
Showing 1 changed file with 40 additions and 3 deletions.
43 changes: 40 additions & 3 deletions src/text_to_sql_search.cr
Expand Up @@ -6,13 +6,23 @@ module TextToSqlSearch
@operators_allowed = ["<", ">", "=", ":"]
@negation_words = ["!", "no", "not"]
@ignored_words = ["with", "than"]
@split_regex = /"(.*?)"|\s+|([()><:=+!-])/
@strip = true
@default_joiner = "AND"
@default_negative = false
@first_element = "1"

def initialize; end

YAML.mapping(
operators_allowed: {type: Array(String)},
negation_words: {type: Array(String)},
ignored_words: {type: Array(String)},
operators_allowed: {type: Array(String)},
negation_words: {type: Array(String)},
ignored_words: {type: Array(String)},
split_regex: {type: Regex},
strip: {type: Bool},
default_joiner: {type: String},
default_negative: {type: Bool},
first_element: {type: String},
)
end

Expand All @@ -33,5 +43,32 @@ module TextToSqlSearch
end
end

def parse( input)
# These happen one time to prepare the input line for parsing
input= input.strip if config.strip
tokens= input.split( config.split_regex).
tokens.reject! {|t| t.blank? }

# These initialize one-time and accumulate values as parsing advances
terms= config.first_element # Accumulated SQL WHERE terms
values= [] of String # Their corresponding values (replacements for "?"s)
count= tokens.size # Total number of tokens we have for parsing
i= 0 # Current position in tokens array

# These are re-set to these same defaults after each key=value is fully processed and a new one begins
joiner= config.default_joiner # Default joiner to put in SQL between (key=value) pairs
negative= config.default_negative # Are we in positive match or negative by default?
content_before= "" # Small accumulator for pass-thru characters like opening parentheses

while i< count
token= tokens[i].strip if config.strip

if config.ignored_words.includes? token
i+= 1
next
end

end
end
end
end

0 comments on commit e3ea183

Please sign in to comment.