# parser: Lexing and parsing OpenType feature files

## Overview

The primary interface for processing `.fea` files is `fontTools.feaLib.parser.Parser`. At a lower level, the `fontTools.feaLib.lexer` module implements feaLib’s lexical analysis of the `.fea` language syntax, augmented by several smaller utility modules.

## Parsing

class fontTools.feaLib.parser.Parser(featurefile, glyphNames=(), followIncludes=True, includeDir=None, **kwargs)

  Bases: `object`

  Initializes a Parser object.

  Example

      from fontTools.feaLib.parser import Parser
      parser = Parser(file, font.getReverseGlyphMap())
      parsetree = parser.parse()

  Note: the `glyphNames` iterable serves a double role to help distinguish glyph names from ranges in the presence of hyphens and to ensure that glyph names referenced in a feature file are actually part of a font’s glyph set. If the iterable is left empty, no glyph name in glyph set checking takes place, and all glyph tokens containing hyphens are treated as literal glyph names, not as ranges. (Adding a space around the hyphen can, in any case, help to disambiguate ranges from glyph names containing hyphens.)

  By default, the parser will follow `include()` statements in the feature file. To turn this off, pass `followIncludes=False`. Pass a directory string as `includeDir` to explicitly declare a directory to search included feature files in.

  extensions = {}
  ast = <module 'fontTools.feaLib.ast' from '/usr/lib/python3/dist-packages/fontTools/feaLib/ast.py'>
  SS_FEATURE_TAGS = {'ss01', 'ss02', 'ss03', 'ss04', 'ss05', 'ss06', 'ss07', 'ss08', 'ss09', 'ss10', 'ss11', 'ss12', 'ss13', 'ss14', 'ss15', 'ss16', 'ss17', 'ss18', 'ss19', 'ss20'}
  CV_FEATURE_TAGS = {'cv01', 'cv02', 'cv03', 'cv04', 'cv05', 'cv06', 'cv07', 'cv08', 'cv09', 'cv10', 'cv11', 'cv12', 'cv13', 'cv14', 'cv15', 'cv16', 'cv17', 'cv18', 'cv19', 'cv20', 'cv21', 'cv22', 'cv23', 'cv24', 'cv25', 'cv26', 'cv27', 'cv28', 'cv29', 'cv30', 'cv31', 'cv32', 'cv33', 'cv34', 'cv35', 'cv36', 'cv37', 'cv38', 'cv39', 'cv40', 'cv41', 'cv42', 'cv43', 'cv44', 'cv45', 'cv46', 'cv47', 'cv48', 'cv49', 'cv50', 'cv51', 'cv52', 'cv53', 'cv54', 'cv55', 'cv56', 'cv57', 'cv58', 'cv59', 'cv60', 'cv61', 'cv62', 'cv63', 'cv64', 'cv65', 'cv66', 'cv67', 'cv68', 'cv69', 'cv70', 'cv71', 'cv72', 'cv73', 'cv74', 'cv75', 'cv76', 'cv77', 'cv78', 'cv79', 'cv80', 'cv81', 'cv82', 'cv83', 'cv84', 'cv85', 'cv86', 'cv87', 'cv88', 'cv89', 'cv90', 'cv91', 'cv92', 'cv93', 'cv94', 'cv95', 'cv96', 'cv97', 'cv98', 'cv99'}
  parse()

    Parse the file, and return a `fontTools.feaLib.ast.FeatureFile` object representing the root of the abstract syntax tree containing the parsed contents of the file.

  parse_anchor_()
  parse_anchor_marks_()
  parse_anchordef_()
  parse_anonymous_()
  parse_attach_()
  parse_enumerate_(vertical)
  parse_GlyphClassDef_()
  parse_glyphclass_definition_()
  split_glyph_range_(name, location)
  parse_glyphclass_(accept_glyphname, accept_null=False)
  parse_glyph_pattern_(vertical)
  parse_ignore_glyph_pattern_(sub)
  parse_ignore_context_(sub)
  parse_ignore_()
  parse_include_()
  parse_language_()
  parse_ligatureCaretByIndex_()
  parse_ligatureCaretByPos_()
  parse_lookup_(vertical)
  parse_lookupflag_()
  parse_markClass_()
  parse_position_(enumerated, vertical)
  parse_position_cursive_(enumerated, vertical)
  parse_position_base_(enumerated, vertical)
  parse_position_ligature_(enumerated, vertical)
  parse_position_mark_(enumerated, vertical)
  parse_script_()
  parse_substitute_()
  parse_subtable_()
  parse_size_parameters_()
  parse_size_menuname_()
  parse_table_()
  parse_table_GDEF_(table)
  parse_table_head_(table)
  parse_table_hhea_(table)
  parse_table_vhea_(table)
  parse_table_name_(table)
  parse_name_()

    Parses a name record. See section 9.e.

  parse_stat_name_()
  parse_nameid_()
  unescape_string_(string, encoding)
  static unescape_unichr_(match)
  static unescape_byte_(match, encoding)
  find_previous(statements, class_)
  parse_table_BASE_(table)
  parse_table_OS_2_(table)
  parse_STAT_ElidedFallbackName()
  parse_STAT_design_axis()
  parse_STAT_axis_value_()
  parse_STAT_location()
  parse_table_STAT_(table)
  parse_base_tag_list_()
  parse_base_script_list_(count)
  parse_base_script_record_(count)
  parse_base_minmax_()
  parse_device_()
  is_next_value_()
  parse_valuerecord_(vertical)
  parse_valuerecord_definition_(vertical)
  parse_languagesystem_()
  parse_feature_block_(variation=False)
  parse_feature_reference_()
  parse_featureNames_(tag)

    Parses a `featureNames` statement found in stylistic set features. See section 8.c.

  parse_cvParameters_(tag)
  parse_cvNameIDs_(tag, block_name)
  parse_cvCharacter_(tag)
  parse_FontRevision_()
  parse_conditionset_()
  parse_block_(block, vertical, stylisticset=None, size_feature=False, cv_feature=None)
  is_cur_keyword_(k)
  expect_class_name_()
  expect_cid_()
  expect_filename_()
  expect_glyph_()
  check_glyph_name_in_glyph_set(*names)

    Adds a glyph name (just start) or glyph names of a range (start and end) which are not in the glyph set to the “missing list” for future error reporting.

    If no glyph set is present, does nothing.

  expect_markClass_reference_()
  expect_tag_()
  expect_script_tag_()
  expect_language_tag_()
  expect_symbol_(symbol)
  expect_keyword_(keyword)
  expect_name_()
  expect_number_(variable=False)
  expect_variable_scalar_()
  expect_master_()
  expect_any_number_()
  expect_float_()
  expect_integer_or_float_()
  expect_decipoint_()
  expect_stat_flags()
  expect_stat_values_()
  expect_string_()
  advance_lexer_(comments=False)
  static reverse_string_(s)

    ‘abc’ –> ‘cba’

  make_cid_range_(location, start, limit)

    (location, 999, 1001) –> [“cid00999”, “cid01000”, “cid01001”]

  make_glyph_range_(location, start, limit)

    (location, “a.sc”, “d.sc”) –> [“a.sc”, “b.sc”, “c.sc”, “d.sc”]

class fontTools.feaLib.parser.SymbolTable

  Bases: `object`

  enter_scope()
  exit_scope()
  define(name, item)
  resolve(name)

## Lexing

class fontTools.feaLib.lexer.IncludingLexer(featurefile, *, includeDir=None)

  Bases: `object`

  A Lexer that follows include statements.

  The OpenType feature file specification states that due to historical reasons, relative imports should be resolved in this order:

  1. If the source font is UFO format, then relative to the UFO’s font directory

  2. relative to the top-level include file

  3. relative to the parent include file

  We only support 1 (via includeDir) and 2.

  static make_lexer_(file_or_path)
  next(self)
  scan_anonymous_block(self, tag)

class fontTools.feaLib.lexer.Lexer(text, filename)

  Bases: `object`

  ANONYMOUS_BLOCK = 'ANONYMOUS_BLOCK'
  CHAR_DIGIT_ = '0123456789'
  CHAR_HEXDIGIT_ = '0123456789ABCDEFabcdef'
  CHAR_LETTER_ = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz'
  CHAR_NAME_CONTINUATION_ = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789_.+*:^~!/-'
  CHAR_NAME_START_ = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz_+*:.^~!\\'
  CHAR_NEWLINE_ = '\r\n'
  CHAR_SYMBOL_ = ",;:-+'{}[]<>()="
  CHAR_WHITESPACE_ = ' \t'
  CID = 'CID'
  COMMENT = 'COMMENT'
  FILENAME = 'FILENAME'
  FLOAT = 'FLOAT'
  GLYPHCLASS = 'GLYPHCLASS'
  HEXADECIMAL = 'HEXADECIMAL'
  MODE_FILENAME_ = 'FILENAME'
  MODE_NORMAL_ = 'NORMAL'
  NAME = 'NAME'
  NEWLINE = 'NEWLINE'
  NUMBER = 'NUMBER'
  NUMBERS = ('NUMBER', 'HEXADECIMAL', 'OCTAL')
  OCTAL = 'OCTAL'
  RE_GLYPHCLASS = re.compile('^[A-Za-z_0-9.\\-]+$')
  STRING = 'STRING'
  SYMBOL = 'SYMBOL'
  location_(self)
  next(self)
  next_(self)
  scan_anonymous_block(self, tag)
  scan_over_(self, valid)
  scan_until_(self, stop_at)

class fontTools.feaLib.lexer.NonIncludingLexer(featurefile, *, includeDir=None)

  Bases: `IncludingLexer`

  Lexer that does not follow include statements, emits them as-is.

## fontTools.feaLib.variableScalar

fontTools.feaLib.variableScalar.Location(loc)

class fontTools.feaLib.variableScalar.VariableScalar(location_value={})

  Bases: `object`

  A scalar with different values at different points in the designspace.

  property does_vary
  property axes_dict
  fix_location(location)
  add_value(location, value)
  fix_all_locations()
  property default
  value_at_location(location, model_cache=None, avar=None)
  model(model_cache=None, avar=None)
  get_deltas_and_supports(model_cache=None, avar=None)
  add_to_variation_store(store_builder, model_cache=None, avar=None)

## fontTools.feaLib.location

class fontTools.feaLib.location.FeatureLibLocation(file: str, line: int, column: int)

  Bases: `NamedTuple`

  A location in a feature file

  file: str

    Alias for field number 0

  line: int

    Alias for field number 1

  column: int

    Alias for field number 2