[voltLib] Add coverage, change group and enum

This commit is contained in:
moyogo 2015-10-05 16:35:34 +02:00
parent 8c0a35331e
commit c26e3228ce
3 changed files with 25 additions and 23 deletions

View File

@ -17,12 +17,10 @@ class GlyphDefinition(ast.Statement):
self.components = components self.components = components
class GroupDefinition(ast.Statement): class GroupDefinition(ast.Statement):
def __init__(self, location, name, glyphs, groups, ranges): def __init__(self, location, name, enum):
ast.Statement.__init__(self,location) ast.Statement.__init__(self,location)
self.name = name self.name = name
self.enum = {"glyphs": glyphs, self.enum = enum
"groups": groups,
"ranges": ranges}
class ScriptDefinition(ast.Statement): class ScriptDefinition(ast.Statement):
def __init__(self, location, name, tag, langs): def __init__(self, location, name, tag, langs):

View File

@ -83,7 +83,7 @@ class Parser(object):
if self.groups_.resolve(name) is not None: if self.groups_.resolve(name) is not None:
raise VoltLibError('Glyph group "%s" already defined' % name, raise VoltLibError('Glyph group "%s" already defined' % name,
location) location)
def_group = ast.GroupDefinition(location, name, **enum) def_group = ast.GroupDefinition(location, name, enum)
self.groups_.define(name, def_group) self.groups_.define(name, def_group)
return def_group return def_group
@ -149,20 +149,24 @@ class Parser(object):
return enum return enum
def parse_coverage_(self): def parse_coverage_(self):
coverage = {'glyphs': [], 'groups': [], 'ranges': []} coverage = []
while self.next_token_ in ("GLYPH", "GROUP", "RANGE"): while self.next_token_ in ("GLYPH", "GROUP", "RANGE"):
if self.next_token_ == "GLYPH": if self.next_token_ == "GLYPH":
self.expect_keyword_("GLYPH") self.expect_keyword_("GLYPH")
name = self.expect_string_() name = self.expect_string_()
coverage['glyphs'].append(name) coverage.append(name)
elif self.next_token_ == "GROUP": elif self.next_token_ == "GROUP":
self.expect_keyword_("GROUP") self.expect_keyword_("GROUP")
name = self.expect_string_() name = self.expect_string_()
coverage['groups'].append(name) group = self.groups_.resolve(name)
if group is None:
raise VoltLibError('Glyph group "%s" is not defined' % name,
location)
coverage.extend(group.enum)
elif self.next_token_ == "RANGE": elif self.next_token_ == "RANGE":
self.expect_keyword_("RANGE") self.expect_keyword_("RANGE")
start, end = self.expect_string_(), self.expect_string_() start, end = self.expect_string_(), self.expect_string_()
coverage['ranges'].append((start, end)) coverage.append((start, end))
return coverage return coverage
def is_cur_keyword_(self, k): def is_cur_keyword_(self, k):

View File

@ -60,21 +60,24 @@ class ParserTest(unittest.TestCase):
).statements ).statements
self.assertEqual((def_group.name, def_group.enum), self.assertEqual((def_group.name, def_group.enum),
("KERN_lc_a_2ND", ("KERN_lc_a_2ND",
{"glyphs": ["a", "aacute", "abreve", "acircumflex", ["a", "aacute", "abreve", "acircumflex", "adieresis",
"adieresis", "ae", "agrave", "amacron", "ae", "agrave", "amacron", "aogonek", "aring",
"aogonek", "aring", "atilde"], "atilde"]))
"groups": [], [def_group1, def_group2] = self.parse(
"ranges": []})) 'DEF_GROUP "aaccented"\n'
[def_group] = self.parse( 'ENUM GLYPH "aacute" GLYPH "abreve" GLYPH "acircumflex" '
'GLYPH "adieresis" GLYPH "ae" GLYPH "agrave" GLYPH "amacron" '
'GLYPH "aogonek" GLYPH "aring" GLYPH "atilde" END_ENUM\n'
'END_GROUP\n'
'DEF_GROUP "KERN_lc_a_2ND"\n' 'DEF_GROUP "KERN_lc_a_2ND"\n'
'ENUM GLYPH "a" GROUP "aaccented" END_ENUM\n' 'ENUM GLYPH "a" GROUP "aaccented" END_ENUM\n'
'END_GROUP' 'END_GROUP'
).statements ).statements
self.assertEqual((def_group.name, def_group.enum), self.assertEqual((def_group2.name, def_group2.enum),
("KERN_lc_a_2ND", ("KERN_lc_a_2ND",
{"glyphs": ["a"], ["a", "aacute", "abreve", "acircumflex", "adieresis",
"groups": ["aaccented"], "ae", "agrave", "amacron", "aogonek", "aring",
"ranges": []})) "atilde"]))
[def_group] = self.parse( [def_group] = self.parse(
'DEF_GROUP "KERN_lc_a_2ND"\n' 'DEF_GROUP "KERN_lc_a_2ND"\n'
'ENUM RANGE "a" "atilde" GLYPH "b" RANGE "c" "cdotaccent" ' 'ENUM RANGE "a" "atilde" GLYPH "b" RANGE "c" "cdotaccent" '
@ -83,10 +86,7 @@ class ParserTest(unittest.TestCase):
).statements ).statements
self.assertEqual((def_group.name, def_group.enum), self.assertEqual((def_group.name, def_group.enum),
("KERN_lc_a_2ND", ("KERN_lc_a_2ND",
{"glyphs": ["b"], [("a", "atilde"), "b", ("c", "cdotaccent")]))
"groups": [],
"ranges": [("a", "atilde"),
("c", "cdotaccent")]}))
def test_group_duplicate(self): def test_group_duplicate(self):
self.assertRaisesRegex( self.assertRaisesRegex(