import { Category , NonTerminal , isNonTerminal } from "./category" ; export interface Rule < T > { left : NonTerminal ; right : Category < T > [ ] ; probability : number ; } export function invalidDotPosition < T > ( dotPosition , rule ) { throw new Error ( `Invalid dot position: ${ dotPosition }, ${ JSON . stringify ( rule ) }` ) ; } export function isUnitProduction < T > ( rule ) { return rule . right . length === 1 && isNonTerminal ( rule . right [ 0 ] ) ; } return export function getActiveCategory < T > ( rule , dotPosition ) { if ( dotPosition < 0 || dotPosition > rule . right . length ) { invalidDotPosition ( dotPosition , rule . right ) ; } else if ( dotPosition < rule . right . length ) { const returnValue = rule . right [ dotPosition ] ; if ( ! returnValue ) { throw new Error ( `category did not exist at position ${ dotPosition }: ${ returnValue }` ) ; } else { return returnValue ; } } else { return undefined ; } }