UNPKG

@flowscripter/mpeg-sdl-parser

Version:

ISO/IEC 14496-34 Syntactic Description Language (MPEG SDL) parser implemented in TypeScript

71 lines (66 loc) 2.22 kB
import { Text } from "@codemirror/state"; import type { TreeCursor } from "@lezer/common"; import { InternalParseError } from "../../ParseError"; import { getChildNodesAndTokens, isAbstractNode, } from "../../util/nodeFactoryUtils"; import { ElementaryType } from "../node/ElementaryType"; import type { Token } from "../token/Token"; import { ElementaryTypeKind } from "../node/enum/elementary_type_kind"; import { NodeKind } from "../node/enum/node_kind"; export function getElementaryType( cursor: TreeCursor, text: Text, ): ElementaryType { const childNodesAndTokens = getChildNodesAndTokens(cursor, text); let elementaryTypeKind: ElementaryTypeKind | undefined; let unsignedQualifierKeyword: Token | undefined; let typeKeyword: Token | undefined; for (const childNodeOrToken of childNodesAndTokens) { if (isAbstractNode(childNodeOrToken)) { throw new InternalParseError( `Unexpected node kind: ${NodeKind[childNodeOrToken.nodeKind]}`, ); } else { switch (childNodeOrToken.text) { case "unsigned": unsignedQualifierKeyword = childNodeOrToken; break; case "bit": elementaryTypeKind = ElementaryTypeKind.BIT; typeKeyword = childNodeOrToken; break; case "int": if (unsignedQualifierKeyword) { elementaryTypeKind = ElementaryTypeKind.UNSIGNED_INTEGER; } else { elementaryTypeKind = ElementaryTypeKind.INTEGER; } typeKeyword = childNodeOrToken; break; case "float": elementaryTypeKind = ElementaryTypeKind.FLOATING_POINT; typeKeyword = childNodeOrToken; break; default: throw new InternalParseError( `Unexpected token: ${childNodeOrToken.text}`, ); } } } if (elementaryTypeKind === undefined) { throw new InternalParseError( "Expected argument elementaryTypeKind to be defined", ); } if (typeKeyword === undefined) { throw new InternalParseError("Expected argument typeKeyword to be defined"); } return new ElementaryType( elementaryTypeKind, unsignedQualifierKeyword, typeKeyword, ); }