1 |
3 |
feddischso |
###############################################################
|
2 |
|
|
#
|
3 |
|
|
# File: ypp.rb
|
4 |
|
|
#
|
5 |
|
|
# Author: Christian Hättich
|
6 |
|
|
#
|
7 |
|
|
# Project: System-On-Chip Maker
|
8 |
|
|
#
|
9 |
|
|
# Target: Linux / Windows / Mac
|
10 |
|
|
#
|
11 |
|
|
# Language: ruby
|
12 |
|
|
#
|
13 |
|
|
#
|
14 |
|
|
###############################################################
|
15 |
|
|
#
|
16 |
|
|
#
|
17 |
|
|
# Copyright (C) 2014 Christian Hättich - feddischson [ at ] opencores.org
|
18 |
|
|
#
|
19 |
|
|
# This program is free software: you can redistribute it and/or modify
|
20 |
|
|
# it under the terms of the GNU General Public License as published by
|
21 |
|
|
# the Free Software Foundation, either version 3 of the License, or
|
22 |
|
|
# (at your option) any later version.
|
23 |
|
|
#
|
24 |
|
|
# This program is distributed in the hope that it will be useful,
|
25 |
|
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
26 |
|
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
27 |
|
|
# GNU General Public License for more details.
|
28 |
|
|
#
|
29 |
|
|
# You should have received a copy of the GNU General Public License
|
30 |
|
|
# along with this program. If not, see .
|
31 |
|
|
#
|
32 |
|
|
#
|
33 |
|
|
###############################################################
|
34 |
|
|
#
|
35 |
|
|
# Description:
|
36 |
|
|
# YAML Pre-Processor:
|
37 |
|
|
# We use it only in a static way, no objects are created
|
38 |
|
|
# (private ctor).
|
39 |
|
|
# There are two methods:
|
40 |
|
|
# - SOCMaker::YPP::to_yaml( a_string )
|
41 |
|
|
# - SOCMaker::YPP::from_yaml( a_string )
|
42 |
|
|
#
|
43 |
|
|
# The function to_yaml replaces our custom tags (like SOCM_CORE)
|
44 |
|
|
# with YAML object identifiers (like '--- ruby/object::SOCMaker::CoreDef')
|
45 |
|
|
#
|
46 |
|
|
# The function from_yaml does the inverse to to_yaml: it replaces the
|
47 |
|
|
# object identifiers with our custom tags.
|
48 |
|
|
#
|
49 |
|
|
# See also SOCMaker::Conf YPP_LUT YPP_INV_LUT and YPP_INV_REGEX,
|
50 |
|
|
# these are the lookup tables and regular expressions used for this
|
51 |
|
|
# processing
|
52 |
|
|
#
|
53 |
|
|
# Goal:
|
54 |
|
|
# This is used to have a nicer YAML file and without the need of writing
|
55 |
|
|
# loong object identifier lines.
|
56 |
|
|
#
|
57 |
|
|
#
|
58 |
|
|
#
|
59 |
|
|
#
|
60 |
|
|
#
|
61 |
|
|
###############################################################
|
62 |
|
|
|
63 |
|
|
|
64 |
|
|
|
65 |
|
|
module SOCMaker
|
66 |
|
|
class YPP
|
67 |
|
|
|
68 |
|
|
|
69 |
|
|
#TODO map should work some how
|
70 |
|
|
class << self
|
71 |
|
|
|
72 |
|
|
|
73 |
|
|
#
|
74 |
|
|
# This function does the pre-processing and
|
75 |
|
|
# replaces our custom tags with yaml-tags, like
|
76 |
|
|
# --- !ruby/object:SOCMaker:: ....
|
77 |
|
|
# In addition, if a block is given, the string
|
78 |
|
|
# is separated into substrings and each sub-string is passed to
|
79 |
|
|
# the block. This allows to process YAML strings, which contain multiple
|
80 |
|
|
# objects. Each object is passed (as yaml string) to the block.
|
81 |
|
|
#
|
82 |
|
|
def to_yaml( string )
|
83 |
|
|
|
84 |
|
|
SOCMaker::conf[ :YPP_LUT ].each do |r, repl|
|
85 |
|
|
string = string.gsub r, repl
|
86 |
|
|
end
|
87 |
|
|
if block_given?
|
88 |
|
|
strings = split_with_match( string, SOCMaker::conf[ :YPP_SPLIT_REGEX ] ) if string != nil
|
89 |
|
|
strings.each{ |x| yield( x ) }
|
90 |
|
|
end
|
91 |
|
|
return string
|
92 |
|
|
end
|
93 |
|
|
|
94 |
|
|
|
95 |
|
|
|
96 |
|
|
def from_yaml( string )
|
97 |
|
|
string.gsub SOCMaker::conf[ :YPP_INV_REGEX ] do |words|
|
98 |
|
|
|
99 |
|
|
# if there is a white-space at the beginning ($1 != nil), we keep
|
100 |
|
|
# the white-space, if there is no white-space we won't keep it
|
101 |
|
|
ws = $1.nil? ? "" : " "
|
102 |
|
|
SOCMaker::conf[ :YPP_INV_LUT ].has_key?( $2 ) ? ws + SOCMaker::conf[ :YPP_INV_LUT ][ $2 ] : words
|
103 |
|
|
end
|
104 |
|
|
end
|
105 |
|
|
|
106 |
|
|
def split_with_match(string, regex)
|
107 |
|
|
indices = []
|
108 |
|
|
strings = []
|
109 |
|
|
return [] if string == nil or string.size == 0
|
110 |
|
|
string.scan( regex ) { |c| indices << $~.offset(0)[0] }
|
111 |
|
|
return [] if indices.size == 0
|
112 |
|
|
indices = [ indices, indices[ 1..-1].map{ |x| x-1 } << string.size-1 ]
|
113 |
|
|
indices[0].zip( indices[1] ).each{ |x| strings<< string[ x[0]..x[1] ] }
|
114 |
|
|
return strings
|
115 |
|
|
end
|
116 |
|
|
|
117 |
|
|
|
118 |
|
|
end
|
119 |
|
|
|
120 |
|
|
private
|
121 |
|
|
def initialize
|
122 |
|
|
end
|
123 |
|
|
|
124 |
|
|
|
125 |
|
|
end # CoreDef
|
126 |
|
|
end # SOCMaker
|
127 |
|
|
|
128 |
|
|
# vim: noai:ts=2:sw=2
|