[voltLib] Allow passing file-like object to Parser

This commit is contained in:
Khaled Hosny 2019-04-04 18:39:07 +02:00
parent 8c2f72118c
commit e082abf627
3 changed files with 18 additions and 23 deletions

View File

@ -39,10 +39,13 @@ class Lexer(object):
if token_type not in {Lexer.NEWLINE}: if token_type not in {Lexer.NEWLINE}:
return (token_type, token, location) return (token_type, token, location)
def location_(self):
column = self.pos_ - self.line_start_ + 1
return (self.filename_ or "<volt>", self.line_, column)
def next_(self): def next_(self):
self.scan_over_(Lexer.CHAR_WHITESPACE_) self.scan_over_(Lexer.CHAR_WHITESPACE_)
column = self.pos_ - self.line_start_ + 1 location = self.location_()
location = (self.filename_, self.line_, column)
start = self.pos_ start = self.pos_
text = self.text_ text = self.text_
limit = len(text) limit = len(text)

View File

@ -32,10 +32,19 @@ class Parser(object):
self.lookups_ = SymbolTable() self.lookups_ = SymbolTable()
self.next_token_type_, self.next_token_ = (None, None) self.next_token_type_, self.next_token_ = (None, None)
self.next_token_location_ = None self.next_token_location_ = None
with open(path, "r") as f: self.make_lexer_(path)
self.lexer_ = Lexer(f.read(), path)
self.advance_lexer_() self.advance_lexer_()
def make_lexer_(self, file_or_path):
if hasattr(file_or_path, "read"):
filename = getattr(file_or_path, "name", None)
data = file_or_path.read()
else:
filename = file_or_path
with open(file_or_path, "r") as f:
data = f.read()
self.lexer_ = Lexer(data, filename)
def parse(self): def parse(self):
statements = self.doc_.statements statements = self.doc_.statements
while self.next_token_type_ is not None: while self.next_token_type_ is not None:

View File

@ -1,12 +1,9 @@
from __future__ import print_function, division, absolute_import from __future__ import print_function, division, absolute_import
from __future__ import unicode_literals from __future__ import unicode_literals
from fontTools.misc.py23 import *
from fontTools.voltLib import ast from fontTools.voltLib import ast
from fontTools.voltLib.error import VoltLibError from fontTools.voltLib.error import VoltLibError
from fontTools.voltLib.parser import Parser from fontTools.voltLib.parser import Parser
from io import open
import os
import shutil
import tempfile
import unittest import unittest
@ -1119,22 +1116,8 @@ class ParserTest(unittest.TestCase):
def_glyph.type, def_glyph.components), def_glyph.type, def_glyph.components),
(".notdef", 0, None, "BASE", None)) (".notdef", 0, None, "BASE", None))
def setUp(self):
self.tempdir = None
self.num_tempfiles = 0
def tearDown(self):
if self.tempdir:
shutil.rmtree(self.tempdir)
def parse(self, text): def parse(self, text):
if not self.tempdir: return Parser(UnicodeIO(text)).parse()
self.tempdir = tempfile.mkdtemp()
self.num_tempfiles += 1
path = os.path.join(self.tempdir, "tmp%d.vtp" % self.num_tempfiles)
with open(path, "w") as outfile:
outfile.write(text)
return Parser(path).parse()
if __name__ == "__main__": if __name__ == "__main__":
import sys import sys