Skip to content

Commit

Permalink
Merge short-to-long mapping into parsed schema data
Browse files Browse the repository at this point in the history
This is stage 1 of rewriting the converter.
  • Loading branch information
happy5214 committed Jun 16, 2024
1 parent 4581431 commit 296c571
Show file tree
Hide file tree
Showing 10 changed files with 309 additions and 251 deletions.
11 changes: 2 additions & 9 deletions common/schema/types.js
Original file line number Diff line number Diff line change
Expand Up @@ -106,11 +106,6 @@ export class Hed3Schema extends Schema {
* @type {SchemaEntries}
*/
entries
/**
* The mapping between short and long tags.
* @type {Mapping}
*/
mapping
/**
* The standard HED schema version this schema is linked to.
* @type {string}
Expand All @@ -122,9 +117,8 @@ export class Hed3Schema extends Schema {
*
* @param {object} xmlData The schema XML data.
* @param {SchemaEntries} entries A collection of schema entries.
* @param {Mapping} mapping A mapping between short and long tags.
*/
constructor(xmlData, entries, mapping) {
constructor(xmlData, entries) {
super(xmlData)

if (!this.library) {
Expand All @@ -133,7 +127,6 @@ export class Hed3Schema extends Schema {
this.withStandard = xmlData.HED?.$?.withStandard
}
this.entries = entries
this.mapping = mapping
}

/**
Expand Down Expand Up @@ -164,7 +157,7 @@ export class PartneredSchema extends Hed3Schema {
* @param {Hed3Schema} actualSchema The actual HED 3 schema underlying this partnered schema.
*/
constructor(actualSchema) {
super({}, actualSchema.entries, actualSchema.mapping)
super({}, actualSchema.entries)
this.actualSchema = actualSchema
this.withStandard = actualSchema.withStandard
this.library = undefined
Expand Down
57 changes: 32 additions & 25 deletions converter/converter.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import castArray from 'lodash/castArray'

import { TagEntry } from './types'
import generateIssue from './issues'
import splitHedString from './splitHedString'

Expand All @@ -23,14 +22,14 @@ export const removeSlashesAndSpaces = function (hedString) {
* on for HED 3 schemas) allow for similar HED 2 validation with minimal code
* duplication.
*
* @param {Schema} schema The schema object containing a short-to-long mapping.
* @param {Hed3Schema} schema The schema object containing a short-to-long mapping.
* @param {string} hedTag The HED tag to convert.
* @param {string} hedString The full HED string (for error messages).
* @param {number} offset The offset of this tag within the HED string.
* @returns {[string, Issue[]]} The long-form tag and any issues.
*/
export const convertTagToLong = function (schema, hedTag, hedString, offset) {
const mapping = schema.mapping
const schemaTags = schema.entries.tags

if (hedTag.startsWith('/')) {
hedTag = hedTag.slice(1)
Expand All @@ -43,15 +42,23 @@ export const convertTagToLong = function (schema, hedTag, hedString, offset) {
const splitTag = cleanedTag.split('/')

/**
* @type {TagEntry}
* @type {SchemaTag}
*/
let foundTagEntry = null
let takesValueTag = false
let endingIndex = 0
let foundUnknownExtension = false
let foundEndingIndex = 0

const generateParentNodeIssue = (tagEntries, startingIndex, endingIndex) => {
/**
* Generate an issue for an invalid parent node.
*
* @param {SchemaTag[]} schemaTags The invalid schema tags.
* @param {number} startingIndex The starting index in the string.
* @param {number} endingIndex The ending index in the string.
* @returns {[string, Issue[]]} The original HED string and the issue.
*/
const generateParentNodeIssue = (schemaTags, startingIndex, endingIndex) => {
return [
hedTag,
[
Expand All @@ -60,11 +67,11 @@ export const convertTagToLong = function (schema, hedTag, hedString, offset) {
hedString,
{
parentTag:
tagEntries.length > 1
? tagEntries.map((tagEntry) => {
return tagEntry.longTag
schemaTags.length > 1
? schemaTags.map((tagEntry) => {
return tagEntry.longName
})
: tagEntries[0].longTag,
: schemaTags[0].longName,
},
[startingIndex + offset, endingIndex + offset],
),
Expand All @@ -79,16 +86,16 @@ export const convertTagToLong = function (schema, hedTag, hedString, offset) {
const startingIndex = endingIndex
endingIndex += tag.length

const tagEntries = castArray(mapping.shortToTags.get(tag))
const tagEntries = castArray(schemaTags.getEntry(tag))

if (foundUnknownExtension) {
if (mapping.shortToTags.has(tag)) {
if (schemaTags.hasEntry(tag)) {
return generateParentNodeIssue(tagEntries, startingIndex, endingIndex)
} else {
continue
}
}
if (!mapping.shortToTags.has(tag)) {
if (!schemaTags.hasEntry(tag)) {
if (foundTagEntry === null) {
return [hedTag, [generateIssue('invalidTag', hedString, {}, [startingIndex + offset, endingIndex + offset])]]
}
Expand All @@ -99,14 +106,14 @@ export const convertTagToLong = function (schema, hedTag, hedString, offset) {

let tagFound = false
for (const tagEntry of tagEntries) {
const tagString = tagEntry.longFormattedTag
const tagString = tagEntry.longName.toLowerCase()
const mainHedPortion = cleanedTag.slice(0, endingIndex)

if (tagString.endsWith(mainHedPortion)) {
tagFound = true
foundEndingIndex = endingIndex
foundTagEntry = tagEntry
if (tagEntry.takesValue) {
if (tagEntry.valueTag) {
takesValueTag = true
}
break
Expand All @@ -118,21 +125,21 @@ export const convertTagToLong = function (schema, hedTag, hedString, offset) {
}

const remainder = hedTag.slice(foundEndingIndex)
const longTagString = foundTagEntry.longTag + remainder
const longTagString = foundTagEntry.longName + remainder
return [longTagString, []]
}

/**
* Convert a HED tag to short form.
*
* @param {Schema} schema The schema object containing a short-to-long mapping.
* @param {Hed3Schema} schema The schema object containing a short-to-long mapping.
* @param {string} hedTag The HED tag to convert.
* @param {string} hedString The full HED string (for error messages).
* @param {number} offset The offset of this tag within the HED string.
* @returns {[string, Issue[]]} The short-form tag and any issues.
*/
export const convertTagToShort = function (schema, hedTag, hedString, offset) {
const mapping = schema.mapping
const schemaTags = schema.entries.tags

if (hedTag.startsWith('/')) {
hedTag = hedTag.slice(1)
Expand All @@ -146,15 +153,15 @@ export const convertTagToShort = function (schema, hedTag, hedString, offset) {
splitTag.reverse()

/**
* @type {TagEntry}
* @type {SchemaTag}
*/
let foundTagEntry = null
let index = hedTag.length
let lastFoundIndex = index

for (const tag of splitTag) {
if (mapping.shortToTags.has(tag)) {
foundTagEntry = mapping.shortToTags.get(tag)
if (schemaTags.hasEntry(tag)) {
foundTagEntry = schemaTags.getEntry(tag)
lastFoundIndex = index
index -= tag.length
break
Expand All @@ -173,12 +180,12 @@ export const convertTagToShort = function (schema, hedTag, hedString, offset) {
}

const mainHedPortion = cleanedTag.slice(0, lastFoundIndex)
const tagString = foundTagEntry.longFormattedTag
const tagString = foundTagEntry.longName.toLowerCase()
if (!tagString.endsWith(mainHedPortion)) {
return [
hedTag,
[
generateIssue('invalidParentNode', hedString, { parentTag: foundTagEntry.longTag }, [
generateIssue('invalidParentNode', hedString, { parentTag: foundTagEntry.longName }, [
index + offset,
lastFoundIndex + offset,
]),
Expand All @@ -187,7 +194,7 @@ export const convertTagToShort = function (schema, hedTag, hedString, offset) {
}

const remainder = hedTag.slice(lastFoundIndex)
const shortTagString = foundTagEntry.shortTag + remainder
const shortTagString = foundTagEntry.name + remainder
return [shortTagString, []]
}

Expand All @@ -196,7 +203,7 @@ export const convertTagToShort = function (schema, hedTag, hedString, offset) {
*
* This is for the internal string parsing for the validation side.
*
* @param {Schema} schema The schema object containing a short-to-long mapping.
* @param {Hed3Schema} schema The schema object containing a short-to-long mapping.
* @param {string} partialHedString The partial HED string to convert to long form.
* @param {string} fullHedString The full HED string.
* @param {number} offset The offset of the partial HED string within the full string.
Expand Down Expand Up @@ -232,7 +239,7 @@ export const convertPartialHedStringToLong = function (schema, partialHedString,
/**
* Convert a HED string.
*
* @param {Schema} schema The schema object containing a short-to-long mapping.
* @param {Hed3Schema} schema The schema object containing a short-to-long mapping.
* @param {string} hedString The HED tag to convert.
* @param {function (Schema, string, string, number): [string, Issue[]]} conversionFn The conversion function for a tag.
* @returns {[string, Issue[]]} The converted string and any issues.
Expand Down
47 changes: 0 additions & 47 deletions converter/schema.js

This file was deleted.

63 changes: 0 additions & 63 deletions converter/types.js

This file was deleted.

11 changes: 4 additions & 7 deletions parser/parsedHedTag.js
Original file line number Diff line number Diff line change
Expand Up @@ -347,7 +347,7 @@ export class ParsedHed3Tag extends ParsedHedTag {
* @returns {string} The nicely formatted version of this tag.
*/
format() {
let tagName = this.schema?.entries.definitions.get('tags').getEntry(this.formattedTag)?.name
let tagName = this.schema?.entries?.tags?.getLongNameEntry(this.formattedTag)?.longName
if (tagName === undefined) {
tagName = this.originalTag
}
Expand All @@ -365,7 +365,7 @@ export class ParsedHed3Tag extends ParsedHedTag {
*/
get existsInSchema() {
return this._memoize('existsInSchema', () => {
return this.schema?.entries.definitions.get('tags').hasEntry(this.formattedTag)
return this.schema?.entries?.tags?.hasLongNameEntry(this.formattedTag)
})
}

Expand Down Expand Up @@ -395,7 +395,7 @@ export class ParsedHed3Tag extends ParsedHedTag {
get takesValueTag() {
return this._memoize('takesValueTag', () => {
if (this.takesValueFormattedTag !== null) {
return this.schema?.entries.definitions.get('tags').getEntry(this.takesValueFormattedTag)
return this.schema?.entries?.tags?.getLongNameEntry(this.takesValueFormattedTag)
} else {
return null
}
Expand All @@ -420,9 +420,6 @@ export class ParsedHed3Tag extends ParsedHedTag {
*/
get hasUnitClass() {
return this._memoize('hasUnitClass', () => {
if (!this.schema?.entries.definitions.has('unitClasses')) {
return false
}
if (this.takesValueTag === null) {
return false
}
Expand Down Expand Up @@ -475,7 +472,7 @@ export class ParsedHed3Tag extends ParsedHedTag {
const tagUnitClasses = this.unitClasses
const units = new Set()
for (const unitClass of tagUnitClasses) {
const unitClassUnits = this.schema?.entries.unitClassMap.getEntry(unitClass.name).units
const unitClassUnits = this.schema?.entries.unitClasses.getEntry(unitClass.name).units
for (const unit of unitClassUnits.values()) {
units.add(unit)
}
Expand Down
Loading

0 comments on commit 296c571

Please sign in to comment.