This repository was archived by the owner on Jan 17, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathparser.py
More file actions
665 lines (613 loc) · 24 KB
/
parser.py
File metadata and controls
665 lines (613 loc) · 24 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
from __future__ import (absolute_import, division, print_function,
unicode_literals)
import re
class FeaToolsParserSyntaxError(Exception):
def __init__(self, value):
self.value = value
def __str__(self):
return repr(self.value)
# used for removing all comments
commentRE = re.compile(r"#.*")
# used for finding all strings
stringRE = re.compile(
r"\"" # "
r"([^\"]*)" # anything but "
r"\"" # "
)
# used for removing all comments
terminatorRE = re.compile(r";")
# used for finding all feature names.
feature_findAll_RE = re.compile(
r"([\s;\{\}]|^)" # whitepace, ; {, } or start of line
r"feature\s+" # feature
r"([\w\d]{4})" # name
r"\s*{" # {
)
# used for finding the content of features.
# this regular expression will be compiled
# for each feature name found.
featureContentRE = [
r"([\s;\{\}]|^)", # whitepace, ; {, } or start of line
r"feature\s+", # feature
# feature name # name
r"\s*\{", # {
r"([\S\s]*?)", # content
r"}\s*", # }
# feature name # name
r"\s*;" # ;
]
# used for finding all lookup names.
lookup_findAll_RE = re.compile(
r"([\s;\{\}]|^)" # whitepace, ; {, } or start of line
r"lookup\s+" # lookup
r"([\w\d_.]+)" # name
r"\s*{" # {
)
# used for finding the content of lookups.
# this regular expression will be compiled
# for each lookup name found.
lookupContentRE = [
r"([\s;\{\}]|^)", # whitepace, ; {, } or start of line
r"lookup\s+", # lookup
# lookup name # name
r"\s*\{", # {
r"([\S\s]*?)", # content
r"}\s*", # }
# lookup name # name
r"\s*;" # ;
]
# used for finding all table names.
table_findAll_RE = re.compile(
r"([\s;\{\}]|^)" # whitepace, ; {, } or start of line
r"table\s+" # table
r"([\w\d/]+)" # name
r"\s*{" # {
)
# used for finding the content of tables.
# this regular expression will be compiled
# for each table name found.
tableContentRE = [
r"([\s;\{\}]|^)", # whitepace, ; {, } or start of line
r"table\s+", # feature
# table name # name
r"\s*\{", # {
r"([\S\s]*?)", # content
r"}\s*", # }
# table name # name
r"\s*;" # ;
]
# used for getting tag value pairs from tables.
tableTagValueRE = re.compile(
r"([\w\d_.]+)" # tag
r"\s+" #
r"([^;]+)" # anything but ;
r";" # ;
)
# used for finding all class definitions.
classDefinitionRE = re.compile(
r"([\s;\{\}]|^)" # whitepace, ; {, } or start of line
r"@" # @
r"([\w\d_.]+)" # name
r"\s*=\s*" # =
r"\[" # [
r"([\w\d\s\-_.@]+)" # content
r"\]" # ]
r"\s*;" # ;
, re.M
)
# used for getting the contents of a class definition
classContentRE = re.compile(
r"([\w\d\-_.@]+)"
)
# used for finding inline classes within a sequence
sequenceInlineClassRE = re.compile(
r"\[" # [
r"([\w\d\s_.@]+)" # content
r"\]" # ]
)
# used for finding all substitution type 1
subType1And2And4RE = re.compile(
r"([\s;\{\}]|^)" # whitepace, ; {, } or start of line
r"substitute|sub\s+" # sub
r"([\w\d\s_.@\[\]]+)" # target
r"\s+by\s+" # by
r"([\w\d\s_.@\[\]]+)" # replacement
r"\s*;" # ;
)
# used for finding all substitution type 3
subType3RE = re.compile(
r"([\s;\{\}]|^)" # whitepace, ; {, } or start of line
r"substitute|sub\s+" # sub
r"([\w\d\s_.@\[\]]+)" # target
r"\s+from\s+" # from
r"([\w\d\s_.@\[\]]+)" # replacement
r"\s*;" # ;
)
# used for finding all ignore substitution type 6
ignoreSubType6RE = re.compile(
r"([\s;\{\}]|^)" # whitepace, ; {, } or start of line
r"ignore\s+substitute|ignore\s+sub\s+" # ignore sub
r"([\w\d\s_.@\[\]']+)" # preceding context, target, trailing context
r"\s*;" # ;
)
# used for finding all substitution type 6
# XXX see failing unit test
subType6RE = re.compile(
r"([\s;\{\}]|^)" # whitepace, ; {, } or start of line
r"substitute|sub\s+" # sub
r"([\w\d\s_.@\[\]']+)" # preceding context, target, trailing context
r"\s+by\s+" # by
r"([\w\d\s_.@\[\]]+)" # replacement
r"\s*;" # ;
)
subType6TargetRE = re.compile(
r"(\[" # [
r"[\w\d\s_.@]+" # content
r"\]" # ]'
r"|" # <or>
r"[\w\d_.@]+)'" # content
)
subType6TargetExtractRE = re.compile(
r"([\w\d_.@]*)" # glyph or class names
)
# used for finding positioning type 1
posType1RE = re.compile(
r"([\s;\{\}]|^)" # whitepace, ; {, } or start of line
r"position|pos\s+" # pos
r"([\w\d\s_.@\[\]]+)" # target
r"\s+<" # <
r"([-\d\s]+)" # value
r"\s*>\s*;" # >;
)
# used for finding positioning type 2
posType2RE = re.compile(
r"([\s;\{\}]|^)" # whitepace, ; {, } or start of line
r"(enum\s+|\s*)" # enum
r"(position|pos\s+)" # pos
r"([-\w\d\s_.@\[\]]+)" # left, right, value
r"\s*;" # ;
)
# used for finding all languagesystem
languagesystemRE = re.compile(
r"([\s;\{\}]|^)" # whitepace, ; {, } or start of line
r"languagesystem\s+" # languagesystem
r"([\w\d]+)" # script tag
r"\s+" #
r"([\w\d]+)" # language tag
r"\s*;" # ;
)
# use for finding all script
scriptRE = re.compile(
r"([\s;\{\}]|^)" # whitepace, ; {, } or start of line
r"script\s+" # script
r"([\w\d]+)" # script tag
r"\s*;" # ;
)
# used for finding all language
languageRE = re.compile(
r"([\s;\{\}]|^)" # whitepace, ; {, } or start of line
r"language\s+" # language
r"([\w\d]+)" # language tag
r"\s*" #
r"([\w\d]*)" # include_dflt or exclude_dflt or nothing
r"\s*;" # ;
)
# use for finding all includes
includeRE = re.compile(
r"([\s;\{\}]|^)" # whitepace, ; {, } or start of line
r"include\s*" # include
r"\(\s*" # (
r"([^\)]+)" # anything but )
r"\s*\)" # )
r"\s*;{0,1}" # ; which will occur zero or one times (ugh!)
)
# used for finding subtable breaks
subtableRE = re.compile(
r"([\s;\{\}]|^)" # whitepace, ; {, } or start of line
r"subtable\s*" # subtable
r"\s*;" # ;
)
# used for finding feature references
featureReferenceRE = re.compile(
r"([\s;\{\}]|^)" # whitepace, ; {, } or start of line
r"feature\s+" # feature
r"([\w\d]{4})" # name
r"\s*;" # {
)
# used for finding lookup references
lookupReferenceRE = re.compile(
r"([\s;\{\}]|^)" # whitepace, ; {, } or start of line
r"lookup\s+" # lookup
r"([\w\d]+)" # name
r"\s*;" # {
)
# use for finding all lookup flags
lookupflagRE = re.compile(
r"([\s;\{\}]|^)" # whitepace, ; {, } or start of line
r"lookupflag\s+" # lookupflag
r"([\w\d,\s]+)" # values
r"\s*;" # ;
)
# used for finding all stylistic set featureNames.
featureNamesRE = re.compile(
r"([\s;\{\}]|^)" # whitepace, ; {, } or start of line
r"featureNames" # featureNames
r"[\S\s]*?}\s*;" # everything up until }; with whitespace
)
def _parseUnknown(writer, text):
text = text.strip()
## extract all table names
tableNames = table_findAll_RE.findall(text)
for precedingMark, tableName in tableNames:
# a regular expression specific to this lookup must
# be created so that nested lookups are safely handled
thisTableContentRE = list(tableContentRE)
thisTableContentRE.insert(2, tableName)
thisTableContentRE.insert(6, tableName)
thisTableContentRE = re.compile("".join(thisTableContentRE))
found = thisTableContentRE.search(text)
tableText = found.group(2)
start, end = found.span()
precedingText = text[:start]
if precedingMark:
precedingText += precedingMark
_parseUnknown(writer, precedingText)
_parseTable(writer, tableName, tableText)
text = text[end:]
## extract all feature names
featureTags = feature_findAll_RE.findall(text)
for precedingMark, featureTag in featureTags:
# a regular expression specific to this lookup must
# be created so that nested lookups are safely handled
thisFeatureContentRE = list(featureContentRE)
thisFeatureContentRE.insert(2, featureTag)
thisFeatureContentRE.insert(6, featureTag)
thisFeatureContentRE = re.compile("".join(thisFeatureContentRE))
found = thisFeatureContentRE.search(text)
featureText = found.group(2)
start, end = found.span()
precedingText = text[:start]
if precedingMark:
precedingText += precedingMark
_parseUnknown(writer, precedingText)
_parseFeature(writer, featureTag, featureText)
text = text[end:]
## extract all lookup names
lookupNames = lookup_findAll_RE.findall(text)
for precedingMark, lookupName in lookupNames:
# a regular expression specific to this lookup must
# be created so that nested lookups are safely handled
thisLookupContentRE = list(lookupContentRE)
thisLookupContentRE.insert(2, lookupName)
thisLookupContentRE.insert(6, lookupName)
thisLookupContentRE = re.compile("".join(thisLookupContentRE))
found = thisLookupContentRE.search(text)
lookupText = found.group(2)
start, end = found.span()
precedingText = text[:start]
if precedingMark:
precedingText += precedingMark
_parseUnknown(writer, precedingText)
_parseLookup(writer, lookupName, lookupText)
text = text[end:]
## extract all class data
classes = classDefinitionRE.findall(text)
for precedingMark, className, classContent in classes:
text = _executeSimpleSlice(precedingMark, text, classDefinitionRE, writer)
className = "@" + className
_parseClass(writer, className, classContent)
## extract substitutions
# sub type 1 and 4
subType1s = subType1And2And4RE.findall(text)
for precedingMark, target, replacement in subType1s:
text = _executeSimpleSlice(precedingMark, text, subType1And2And4RE, writer)
_parseSubType1And2And4(writer, target, replacement)
# sub type 3
subType3s = subType3RE.findall(text)
for precedingMark, target, replacement in subType3s:
text = _executeSimpleSlice(precedingMark, text, subType3RE, writer)
_parseSubType3(writer, target, replacement)
# sub type 6
subType6s = subType6RE.findall(text)
for precedingMark, target, replacement in subType6s:
text = _executeSimpleSlice(precedingMark, text, subType6RE, writer)
_parseSubType6(writer, target, replacement)
# ignore sub type 6
ignoreSubType6s = ignoreSubType6RE.findall(text)
for precedingMark, target in ignoreSubType6s:
text = _executeSimpleSlice(precedingMark, text, ignoreSubType6RE, writer)
_parseSubType6(writer, target, replacement=None, ignore=True)
## extract positions
# pos type 1
posType1s = posType1RE.findall(text)
for precedingMark, target, value in posType1s:
text = _executeSimpleSlice(precedingMark, text, posType1RE, writer)
_parsePosType1(writer, target, value)
# pos type 2
posType2s = posType2RE.findall(text)
for precedingMark, enumTag, posTag, targetAndValue in posType2s:
text = _executeSimpleSlice(precedingMark, text, posType2RE, writer)
_parsePosType2(writer, targetAndValue, needEnum=enumTag.strip())
## extract other data
# XXX look at FDK spec. sometimes a language tag of dflt will be passed
# it should be handled differently than the other tags.
# languagesystem
languagesystems = languagesystemRE.findall(text)
for precedingMark, scriptTag, languageTag in languagesystems:
text = _executeSimpleSlice(precedingMark, text, languagesystemRE, writer)
writer.languageSystem(languageTag, scriptTag)
# script
scripts = scriptRE.findall(text)
for precedingMark, scriptTag in scripts:
text = _executeSimpleSlice(precedingMark, text, scriptRE, writer)
writer.script(scriptTag)
# language
languages = languageRE.findall(text)
for precedingMark, languageTag, otherKeyword in languages:
text = _executeSimpleSlice(precedingMark, text, languageRE, writer)
if not otherKeyword or otherKeyword == "include_dflt":
writer.language(languageTag)
elif otherKeyword == "exclude_dflt":
writer.language(languageTag, includeDefault=False)
# include
inclusions = includeRE.findall(text)
for precedingMark, path in inclusions:
text = _executeSimpleSlice(precedingMark, text, includeRE, writer)
writer.include(path)
# feature reference
featureReferences = featureReferenceRE.findall(text)
for precedingMark, featureTag in featureReferences:
text = _executeSimpleSlice(precedingMark, text, featureReferenceRE, writer)
writer.featureReference(featureTag)
# lookup reference
lookupReferences = lookupReferenceRE.findall(text)
for precedingMark, lookupName in lookupReferences:
text = _executeSimpleSlice(precedingMark, text, lookupReferenceRE, writer)
writer.lookupReference(lookupName)
# lookupflag
lookupflags = lookupflagRE.findall(text)
for precedingMark, lookupflagValues in lookupflags:
text = _executeSimpleSlice(precedingMark, text, lookupflagRE, writer)
_parseLookupFlag(writer, lookupflagValues)
# subtable break
subtables = subtableRE.findall(text)
for precedingMark in subtables:
text = _executeSimpleSlice(precedingMark, text, subtableRE, writer)
writer.subtableBreak()
## extract all featureNames
featureNames = featureNamesRE.findall(text)
for precedingMark in featureNames:
text = _executeSimpleSlice(precedingMark, text, featureNamesRE, writer)
# empty instructions
terminators = terminatorRE.findall(text)
for terminator in terminators:
text = _executeSimpleSlice(None, text, terminatorRE, writer)
writer.rawText(terminator)
text = text.strip()
if text:
raise FeaToolsParserSyntaxError("Invalid Syntax: %s" % text)
def _executeSimpleSlice(precedingMark, text, regex, writer):
first = regex.search(text)
start, end = first.span()
precedingText = text[:start]
if precedingMark:
precedingText += precedingMark
_parseUnknown(writer, precedingText)
text = text[end:]
return text
def _parseFeature(writer, name, feature):
featureWriter = writer.feature(name)
parsed = _parseUnknown(featureWriter, feature)
def _parseLookup(writer, name, lookup):
lookupWriter = writer.lookup(name)
parsed = _parseUnknown(lookupWriter, lookup)
def _parseTable(writer, name, table):
tagValueTables = ["GDEF", "head", "hhea", "OS/2", "vhea"]
# skip unknown tables
if name not in tagValueTables:
return
_parseTagValueTable(writer, name, table)
def _parseTagValueTable(writer, name, table):
valueTypes = {
"GDEF" : {
"GlyphClassDef" : str
},
"head" : {
"FontRevision" : float
},
"hhea" : {
"CaretOffset" : float,
"Ascender" : float,
"Descender" : float,
"LineGap" : float,
},
"OS/2" : {
"FSType" : int,
"Panose" : "listOfInts",
"UnicodeRange" : "listOfInts",
"CodePageRange" : "listOfInts",
"TypoAscender" : float,
"TypoDescender" : float,
"TypoLineGap" : float,
"winAscent" : float,
"winDescent" : float,
"XHeight" : float,
"CapHeight" : float,
"WeightClass" : float,
"WidthClass" : float,
"Vendor" : str
},
"vhea" : {
"VertTypoAscender" : float,
"VertTypoDescender" : float,
"VertTypoLineGap" : float
}
}
tableTypes = valueTypes[name]
parsedTagValues = []
for tag, value in tableTagValueRE.findall(table):
tag = tag.strip()
value = value.strip()
if tag not in tableTypes:
raise FeaToolsParserSyntaxError("Unknown Tag: %s" % tag)
desiredType = tableTypes[tag]
if desiredType == "listOfInts":
v = []
for line in value.splitlines():
for i in line.split():
v.append(i)
value = v
values = []
for i in value:
try:
i = int(i)
values.append(i)
except ValueError:
raise FeaToolsParserSyntaxError("Invalid Syntax: %s" % i)
value = values
elif not isinstance(value, desiredType):
try:
value = desiredType(value)
except ValueError:
raise FeaToolsParserSyntaxError("Invalid Syntax: %s" % i)
parsedTagValues.append((tag, value))
writer.table(name, parsedTagValues)
def _parseClass(writer, name, content):
content = classContentRE.findall(content)
writer.classDefinition(name, content)
def _parseSequence(sequence):
parsed = []
for content in sequenceInlineClassRE.findall(sequence):
first = sequenceInlineClassRE.search(sequence)
start, end = first.span()
precedingText = sequence[:start]
parsed.extend(_parseSequence(precedingText))
parsed.append(_parseSequence(content))
sequence = sequence[end:]
content = [i for i in sequence.split(" ") if i]
parsed.extend(content)
return parsed
def _parseSubType1And2And4(writer, target, replacement):
target = _parseSequence(target)
replacement = _parseSequence(replacement)
if len(target) > 1 and len(replacement) > 1:
raise FeaToolsParserSyntaxError("many to many replacement are not allowed")
if len(target) == 1 and len(replacement) == 1:
# replacement will always be one item.
# either a single glyph/class or a list
# reresenting an inline class.
target = target[0]
replacement = replacement[0]
writer.gsubType1(target, replacement)
elif len(replacement) == 1:
# target will always be a list representing a sequence.
# the list may contain strings representing a single
# glyph/class or a list representing an inline class.
replacement = replacement[0]
writer.gsubType4(target, replacement)
else:
target = target[0]
writer.gsubType2(target, replacement)
def _parseSubType3(writer, target, replacement):
# target will only be one item representing
# a glyph/class name.
target = classContentRE.findall(target)
target = target[0]
replacement = classContentRE.findall(replacement)
writer.gsubType3(target, replacement)
def _parseSubType6(writer, target, replacement=None, ignore=False):
# replacement will always be one item.
# either a single glyph/class or a list
# representing an inline class.
# the only exception to this is if
# this is an ignore substitution.
# in that case, replacement will
# be None.
if not ignore:
replacement = classContentRE.findall(replacement)
if len(replacement) == 1:
replacement = replacement[0]
#
targetText = target
#
precedingContext = ""
targets = subType6TargetRE.findall(targetText)
trailingContext = ""
#
targetCount = len(targets)
counter = 1
extractedTargets = []
for target in targets:
first = subType6TargetRE.search(targetText)
start, end = first.span()
if counter == 1:
precedingContext = _parseSequence(targetText[:start])
if counter == targetCount:
trailingContext = _parseSequence(targetText[end:])
# the target could be in a form like [o o.alt]
# so it has to be broken down
target = classContentRE.findall(target)
if len(target) == 1:
target = target[0]
extractedTargets.append(target)
counter += 1
targetText = targetText[end:]
writer.gsubType6(precedingContext, extractedTargets, trailingContext, replacement)
def _parsePosType1(writer, target, value):
# target will only be one item representing
# a glyph/class name
value = tuple([float(i) for i in value.strip().split(" ")])
writer.gposType1(target, value)
def _parsePosType2(writer, targetAndValue, needEnum=False):
# the target and value will be coming
# in as single string.
target = " ".join(targetAndValue.split(" ")[:-1])
value = targetAndValue.split(" ")[-1]
# XXX this could cause a choke
value = float(value)
target = _parseSequence(target)
writer.gposType2(target, value, needEnum)
def _parseLookupFlag(writer, values):
values = values.replace(",", " ")
values = [i for i in values.split(" ") if i]
# lookupflag format B is not supported except for value 0
if len(values) == 1:
try:
v = int(values[0])
if v != 0:
raise FeaToolsParserSyntaxError("lookupflag format B is not supported for any value other than 0")
else:
writer.lookupFlag()
return
except ValueError:
pass
rightToLeft = False
ignoreBaseGlyphs = False
ignoreLigatures = False
ignoreMarks = False
possibleValues = ["RightToLeft", "IgnoreBaseGlyphs", "IgnoreLigatures", "IgnoreMarks"]
for value in values:
if value not in possibleValues:
raise FeaToolsParserSyntaxError("Unknown lookupflag value: %s" % value)
if value == "RightToLeft":
rightToLeft = True
elif value == "IgnoreBaseGlyphs":
ignoreBaseGlyphs = True
elif value == "IgnoreLigatures":
ignoreLigatures = True
elif value == "IgnoreMarks":
ignoreMarks = True
writer.lookupFlag(rightToLeft=rightToLeft, ignoreBaseGlyphs=ignoreBaseGlyphs, ignoreLigatures=ignoreLigatures, ignoreMarks=ignoreMarks)
def parseFeatures(writer, text):
# strip the strings.
# (an alternative approach would be to escape the strings.
# the problem is that a string could contain parsable text
# that would fool the parsing algorithm.)
text = stringRE.sub(r"", text)
# strip the comments
text = commentRE.sub(r"", text)
# make sure there is a space after all ;
# since it makes the text more digestable
# for the regular expressions
text = terminatorRE.sub(r"; ", text)
_parseUnknown(writer, text)