Parse part of file and ignore anything else [message #1744925] |
Mon, 03 October 2016 02:14  |
Jair Garcia Messages: 4 Registered: September 2016 |
Junior Member |
|
|
Hi.
Im working on an island parser for Ruby on rails models.
I'm only interested in parsing part of some classes :
- Class definition, i.e. "class Student < Things::Persons". The line always starts with 'class', then class name, then '<' and superclass name.
- Table name assignation i.e "set_table_name 'people'", the line always starts with "set_table_name" and then a string.
- Class associations i.e: "belongs_to :school", "has_many 'groups', class_name: 'Group'". The line always starts with "belongs_to", "has_many", "has_one" or "has_and_belongs_to_many", then relationship name, and options
- Method definition i.e: "def method_name". The line always starts with "def", then method name and params. I don't want to get method params, method content or where it finishes.
2, 3 and 4 can appear in any order in the code.
I have created a xtext parser for get these kind of lines, but ruby language is too complicated.
Some times when I trying to capture a line of code, it get more than one and hides some lines of the lines that I need (one of the four types)
I'm struggling to create more complex rules (and terminals) for keep my important lines and ignore other, but more and more errors appear.
There is a easy way to parse only some rules and ignore anything else.
I attached a code sample (comments starts with #) and my current grammar.
Thanks in advance
Attachment: Rails.xtext
(Size: 4.84KB, Downloaded 82 times)
|
|
|
Sample [message #1744926 is a reply to message #1744925] |
Mon, 03 October 2016 02:17  |
Jair Garcia Messages: 4 Registered: September 2016 |
Junior Member |
|
|
Sample file:
# Single line comment
# Single line comment
=begin
Multiline comment.
=end
require 'external_libraries'
#Class name is Student, superclase ActiveRecord::Base
class Student < ActiveRecord::Base
#Important lines
#relationships
belongs_to :school
has_many :courses, :class_name=>'Group'
#table_name
set_table_name 'table_name'
#method_definition
def self.method1(param1,param2,param3)
a=1+2
end
def method2? param1, param2
param=param1+param2
end
#rubish lines of code
garbage_line :order => 'xx'
other_line
CONSTANT = [1,2,3]
CONSTANT1 = {a:1,b:2}
CONSTANT2 = 1
CONSTANT3 = "a"
validates_presence_of :name, :if => Proc.new{|obj| obj.step_gt_or_eq(2) }
@init||=1
regular_expression||=/[0-9]/
boolean_var = self.start && CONSTANT <= var
end
Attachment: sample.rb
(Size: 0.78KB, Downloaded 60 times)
|
|
|
Powered by
FUDForum. Page generated in 0.01854 seconds