Skip to content

Types and inheritance

Texomy types form a hierarchy. A subtype inherits from its parent and can stand in for it, so a spec grows by adding types, not by rewriting patterns.

A hierarchy of types

Nested keys declare subtypes. In this spec, Currency::USD and Currency::EUR are subtypes of Currency, and Number::Integer and Number::Decimal are subtypes of Number.

Currency:
  USD: [ 'USD', '\$' ]
  EUR: [ 'EUR', '€' ]

Number:
  Integer: '\d+'
  Decimal: '\d+\.\d+'

Money:
  $fields:
    currency: Currency
    amount: Number
  $patterns:
    - '#{currency}\s?#{amount}'
    - '#{amount}\s?#{currency}'

A nested declaration and a qualified one, Currency::USD: 'USD', declare the same subtype. Every result node carries its concrete type, so code downstream can handle Currency::EUR on its own or treat every Currency alike.

A subtype fits where its parent fits

The field currency of Money is declared as Currency. Any subtype of Currency can fill it. Add one line for yen:

money.yml+1
Currency:  USD: [ 'USD', '\$' ]  EUR: [ 'EUR', '€' ]  JPY: [ 'JPY', '¥' ] Money:  $fields:    currency: Currency    amount: Number

Nothing else changes. Money now accepts yen, and so does every spec that imports money:

invoice.yml
$imports: 'money'

InvoiceTotal:
  $fields: { total: Money }
  $patterns: 'Total: #{total}'
Total: ¥18400
- type: 'InvoiceTotal'
  start: 0
  end: 13
  fields:
    total:
      type: 'Money'
      start: 7
      end: 13
      fields:
        currency: { type: 'Currency::JPY', start: 7, end: 8, text: '¥' }
        amount: { type: 'Number::Integer', start: 8, end: 13, text: '18400' }

Subtypes inherit fields and patterns

Fields, patterns and values declared on a type belong to all its subtypes. In the invoices example, Amounts declares its field, its pattern and its value once. Each kind of amount is a subtype that declares only its label:

invoices.yml, excerpt
  # An amount the invoice states: each kind declares its label.
  Amounts:
    $fields: { amount: Money::Amount }
    $patterns: '${Label}${Is}#{amount}'
    $value: { amount: '#{amount.amount}', currency: '#{amount.currency}' }

    Subtotal:
      $local:
        Label: [ 'Sub-?total' ]

    Tax:
      $local:
        # Sales tax, VAT 20%, Tax (8%)
        Label: [ '((Sales )?tax|VAT|GST)( \(?\d+(\.\d+)?%\)?)?' ]

    Payable:
      $local:
        Label:
          - 'Amount (payable|due)'
          - 'Total( amount)? (payable|due)'
          - 'Balance due'
          - 'Please pay'
          - '(Grand|Invoice) total'
          - 'Total'

  # When payment is due, counted in days: Net 30, payable within 30 days.
  PaymentTerms:
    $local: { Days: '\d+' }
    $fields: { days: Days }
    $patterns:
      - 'Net ?#{days}'
      - '(payable |due )?(within|in) #{days} days'
    $value: '#{days}'

Amounts itself matches nothing: its pattern needs a Label that only its subtypes declare. A new kind of amount, such as a discount, is one more subtype with its label.

Narrowing

A subtype can redeclare an inherited field with a subtype of its original type. In the invoices example, Date declares month: Month; Date::Iso narrows it to Month::Numeric and Date::DayMonth to Month::Name, while the field keeps its name and place in the result.

A single occurrence in a pattern can narrow a field too. #{currency:Currency::EUR} accepts euros only, here and nowhere else:

EuroRefund:
  $fields:
    currency: Currency
    amount: Number
  $patterns: 'refund #{currency:Currency::EUR}\s?#{amount}'

refund €40 is a EuroRefund. refund $50 is not, and stays an ordinary Money.

Values and flags are inherited too

A subtype inherits $value unless it declares its own. Here Number::Decimal builds its value with the template it inherits from Number, so 12,50 becomes 12.50:

Number:
  $local: { Digits: '\d+' }
  $fields: { whole: Digits, cents: Digits }
  $value: '#{whole}[.#{cents}]'
  Integer: '#{whole}'
  Decimal: '#{whole}[.,]#{cents}'
refund €12,50, the amount
amount:
  type: 'Number::Decimal'
  start: 8
  end: 13
  fields:
    whole: { type: 'Digits', start: 8, end: 10, text: '12' }
    cents: { type: 'Digits', start: 11, end: 13, text: '50' }
  value: '12.50'

Flags such as CASE_INSENSITIVE pass from a file or type to its descendants. A descendant can turn an inherited flag off with -FLAG.

Local types

Types declared under $local are visible only to the declaring type and its descendants. In the invoices example, Is, Name and Code serve Invoice and its facts, and do not match anywhere else in the input.

Why it matters

A parser grows with its inputs: a new supplier, a new kind of amount, a new currency. With a hierarchy, each of them is a new type that reuses what is already declared. Shared fields stay in one place, every pattern that references a parent picks up its new subtypes, and the tests of the existing cases show that nothing else changed.

A script from your AI can do the same work, but the reuse is whatever its author built that day. Compare both on the same change in Texomy vs a script from your AI.


The full syntax is in the language reference. Try the hierarchy yourself on the invoices example, where dates, amounts and references each have one pattern and a subtype per kind.