Code generator for C++ from YAML to generate network protocol parsers
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 

137 lines
2.4 KiB

require "ecr"
require "yaml"
module Andrew
VERSION = "0.1.0"
# TODO: Put your code here
end
class FileGenerator
YAML.mapping(
classes: Array(ClassGenerator),
includes: Array(String)
)
def initialize
@classes = Array(ClassGenerator).new
@includes = Array(String).new
end
ECR.def_to_s "src/views/file.ecr"
end
class ClassGenerator
YAML.mapping(
name: String,
bitfields: {
type: Array(BitfieldGenerator),
nilable: true
},
attributes: {
type: Array(AttributeGenerator),
nilable: true
},
repeats: {
type: Array(RepeatGenerator),
nilable: true
},
attributeafters: {
type: Array(AttributeAfterGenerator),
nilable: true
}
)
def initialize(@name : String)
@bitfields = Array(BitfieldGenerator).new
@attributes = Array(AttributeGenerator).new
@repeats = Array(RepeatGenerator).new
@attributeafters = Array(AttributeAfterGenerator).new
end
def push(attr : AttributeGenerator)
attr.class_name = @name
@attributes.push(attr)
end
def push(bitf : BitfieldGenerator)
end
ECR.def_to_s "src/views/class.ecr"
end
class BitfieldGenerator
YAML.mapping(
class_name: {
type: String,
default: "no_class"
},
name: String,
out_type: String,
start: Int32,
size: Int32
)
def initialize(@name : String, @out_type : String, @start : Int32, @size : Int32)
@class_name = "no_class"
end
end
class AttributeGenerator
YAML.mapping(
class_name: {
type: String,
default: "no_class"
},
name: String,
out_type: String,
start: Int32
)
def initialize(@name : String, @out_type : String, @start : Int32)
@class_name = "no_class"
end
ECR.def_to_s "src/views/attribute.ecr"
end
class RepeatGenerator
YAML.mapping(
class_name: {
type: String,
default: "no_class"
},
name: String,
out_type: String,
start: Int32,
count_name: String
)
def initialize(@name : String, @out_type : String, @start : Int32, @count_name : String)
@class_name = "no_class"
end
end
class AttributeAfterGenerator
YAML.mapping(
class_name: {
type: String,
default: "no_class"
},
name: String,
out_type: String,
start: Int32,
after_name: String
)
def initialize(@name : String, @out_type : String, @start : Int32, @after_name : String)
@class_name = "no_class"
end
end
if(ARGV.size != 0)
File.open(ARGV[0]) do |file|
puts FileGenerator.from_yaml(file).to_s
end
else
puts "Andrew code generator\n\tExpects a single YAML file as argument"
end