Permalink
Browse files

Fix yaml-string remapping from quoted strings to native strings.

Besides being less verbose than depending on a custom tag for native strings,
it should fix an issue where referenced yaml blocks lead to overquoted strings in the templates like """"""""""SomeConstant""""""""""

Quoted strings for c++ string literals can be set as quotes within forced yaml strings i.e. '"literal"' or "\"literal\""
In a similar way chars could be specified, just with different quotes on the inside: "'c'" or '\'c\''
  • Loading branch information...
t4im committed Nov 29, 2018
1 parent b5b85b9 commit 80d36f22c16ed7a7a321a9f48d1e8e994b85d841
Showing with 8 additions and 25 deletions.
  1. +0 −19 CBSE.py
  2. +8 −6 def.yaml
19 CBSE.py
@@ -37,14 +37,6 @@
import os.path
import re

# A data container created from the YAML 'plain' tag
class Plain:
def __init__(self, data):
self.data = data

def value(self):
return self.data

class CommonAttribute:
def __init__(self, name, typ):
self.name = name
@@ -316,15 +308,9 @@ def get_message_handler_name(self, message):

def convert_params(params):
# Convert defaults to strings that C++ understands.
# TODO: Add support for char and others
# WORKAROUND: Can specify char x via !!python/object/apply:builtins.ord ['x']
for param, value in params.items():
if type(value) == bool:
value = str(value).lower()
elif type(value) == str:
value = '"' + value + '"'
elif isinstance(value, Plain):
value = value.value()
elif value is None:
pass
else:
@@ -543,14 +529,9 @@ def OrderedMapping(loader, node):
loader.flatten_mapping(node)
return OrderedDict(loader.construct_pairs(node))

@staticmethod
def MakePlain(loader, node):
return Plain(node.value)

def __init__(self, stream):
yaml.Loader.__init__(self, stream)
self.add_constructor(yaml.resolver.BaseResolver.DEFAULT_MAPPING_TAG, self.OrderedMapping)
self.add_constructor(u'tag:yaml.org,2002:plain', self.MakePlain)


if __name__ == '__main__':
@@ -77,16 +77,16 @@ components:
anInt: int
aBool: bool
defaults:
aConstant: !!plain __LINE__
aString: "I am a string"
aConstant: __LINE__
aString: '"I am a string"'
anInt: 23
aBool: true

TypeTest2:
requires:
TypeTest1:
aConstant: !!plain __LINE__
aString: "I am a string"
aConstant: __LINE__
aString: '"I am a string literal"'
anInt: 23
aBool: true

@@ -118,8 +118,10 @@ entities:
TypeTest3:
components:
TypeTest1:
aConstant: !!plain __LINE__
aString: "I am a string"
aConstant: __LINE__
someCode: '&*yaml::conflicting::symbols[0]'
aString: '"I am a string literal"'
aChar: "'a'"
anInt: 23
aBool: true

0 comments on commit 80d36f2

Please sign in to comment.