@neutrium/quantity - v7.1.0
    Preparing search index...

    Class NearleyParser

    Parser for scalar products/division, compound units, powers, and nested groups. Powers bind first, then dot products; * / and whitespace evaluate left to right. Compiled expression plans are shared within a configuration; scalar evaluation follows the original operation order and the selected numeric adapter on every call.

    Hierarchy

    • ExpressionParser
      • NearleyParser
    Index
    • Create a parser with optional cache limits. Omitted options share the default configuration. Separately supplied option objects isolate retention; passing an already resolved configuration reuses it.

      Parameters

      Returns NearleyParser

      const parser = new NearleyParser({ cache: { maxEntries: 128 } });
      parser.config.cache.maxEntries; // 128
    • Release expression plans shared by this configuration; does not clear token or Quantity result caches.

      Returns void

    • Parse and evaluate a complete scalar/unit expression.

      Type Parameters

      • T

      Parameters

      • input: string

        Expression supporting products, division, powers, groups, scientific notation, and Unicode multiplication/superscript notation.

      • Numeric: ArithmeticAdapter<T>

        Adapter used to evaluate scalars; supplied automatically by a configured Quantity.

      • Optionalscope: CacheScope

        Optional Quantity cache scope; wrappers should forward it unchanged.

      • Optionalscalar: T

        Optional replacement scalar. The parser validates the units and skips scalar evaluation.

      Returns QuantityDefinition<T>

      A fresh definition with a backend scalar and frozen counted units. Unit arrays may be shared with cached plans.

      QuantityParseError for invalid or incomplete syntax, unknown units, invalid exponents, and scalar-evaluation failures.

      Physical checks such as absolute zero belong to Quantity construction.

      import { PrattParser } from '@neutrium/quantity/parsers/pratt';
      import { NumberAdapter } from '@neutrium/math/adapters/number';
      const numberAdapter = new NumberAdapter();

      const parsed = new PrattParser().parse('1/2 m', numberAdapter);
      parsed.scalar; // 0.5
    • Parse without throwing for syntax or expression-evaluation failures.

      Type Parameters

      • T

      Parameters

      • input: string

        Complete scalar/unit expression.

      • Numeric: ArithmeticAdapter<T>

        Backend used to evaluate the scalar.

      Returns QuantityParseResult<QuantityDefinition<T>>

      A definition on success, or a structured error at the original location.

      Like parse, this does not check absolute-temperature physics; construct a Quantity from the successful definition for those checks.

      import { PrattParser } from '@neutrium/quantity/parsers/pratt';
      import { Quantity } from '@neutrium/quantity/number';
      const result = new PrattParser().tryParse('kg/(m*s', new Quantity(1).numeric);
      if (!result.success) {
      result.error.code; // "UNEXPECTED_END"
      result.error.offset; // 7 (the end of the input)
      result.error.expected; // [')']
      }