Skip to content

Commit

Permalink
Remove HED 2 support (stage 1) and reorganize code
Browse files Browse the repository at this point in the history
  • Loading branch information
happy5214 committed Nov 6, 2024
1 parent 84fd711 commit 28e9d7a
Show file tree
Hide file tree
Showing 45 changed files with 1,062 additions and 2,063 deletions.
4 changes: 2 additions & 2 deletions bids/schema.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import castArray from 'lodash/castArray'
import semver from 'semver'

import { buildSchemas } from '../validator/schema/init'
import { buildSchemas } from '../schema/init'
import { generateIssue, IssueError } from '../common/issues/issues'
import { SchemaSpec, SchemasSpec } from '../common/schema/types'
import { SchemaSpec, SchemasSpec } from '../schema/specs'

const alphabeticRegExp = new RegExp('^[a-zA-Z]+$')

Expand Down
5 changes: 5 additions & 0 deletions common/issues/data.js
Original file line number Diff line number Diff line change
Expand Up @@ -317,6 +317,11 @@ export default {
level: 'error',
message: stringTemplate`Lazy partnered schemas are incompatible because they share the short tag "${'tag'}". These schemas require different prefixes.`,
},
deprecatedStandardSchemaVersion: {
hedCode: 'VERSION_DEPRECATED',
level: 'error',
message: stringTemplate`HED standard schema version ${'version'} is deprecated. Please upgrade to a newer version.`,
},
// BIDS issues
sidecarKeyMissing: {
hedCode: 'SIDECAR_KEY_MISSING',
Expand Down
13 changes: 0 additions & 13 deletions common/schema/config.js

This file was deleted.

11 changes: 4 additions & 7 deletions parser/parsedHedGroup.js
Original file line number Diff line number Diff line change
@@ -1,16 +1,15 @@
import differenceWith from 'lodash/differenceWith'

import { generateIssue, IssueError } from '../common/issues/issues'
import { getParsedParentTags } from '../utils/hedData'
import { IssueError } from '../common/issues/issues'
import { getTagName } from '../utils/hedStrings'
import ParsedHedSubstring from './parsedHedSubstring'
import { ParsedHed3Tag, ParsedHedTag } from './parsedHedTag'
import ParsedHedTag from './parsedHedTag'
import ParsedHedColumnSplice from './parsedHedColumnSplice'

/**
* A parsed HED tag group.
*/
export class ParsedHedGroup extends ParsedHedSubstring {
export default class ParsedHedGroup extends ParsedHedSubstring {
static SPECIAL_SHORT_TAGS = new Set(['Definition', 'Def', 'Def-expand', 'Onset', 'Offset', 'Inset'])

/**
Expand Down Expand Up @@ -74,7 +73,7 @@ export class ParsedHedGroup extends ParsedHedSubstring {
return undefined
}
const tags = group.tags.filter((tag) => {
if (!(tag instanceof ParsedHed3Tag)) {
if (!(tag instanceof ParsedHedTag)) {
return false
}
const schemaTag = tag.schemaTag
Expand Down Expand Up @@ -540,5 +539,3 @@ export class ParsedHedGroup extends ParsedHedSubstring {
}
}
}

export default ParsedHedGroup
2 changes: 1 addition & 1 deletion parser/parsedHedString.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { ParsedHedTag } from './parsedHedTag'
import ParsedHedTag from './parsedHedTag'
import ParsedHedGroup from './parsedHedGroup'
import ParsedHedColumnSplice from './parsedHedColumnSplice'

Expand Down
4 changes: 2 additions & 2 deletions parser/parsedHedSubstring.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Memoizer } from '../utils/types'
import Memoizer from '../utils/memoizer'

/**
* A parsed HED substring.
Expand Down Expand Up @@ -34,7 +34,6 @@ export class ParsedHedSubstring extends Memoizer {
*
* @param {ParsedHedTag|string} parent The possible parent tag.
* @return {boolean} Whether {@code parent} is the parent tag of this tag.
* @abstract
*/
// eslint-disable-next-line no-unused-vars
isDescendantOf(parent) {
Expand All @@ -50,6 +49,7 @@ export class ParsedHedSubstring extends Memoizer {
* @returns {string}
* @abstract
*/
// eslint-disable-next-line no-unused-vars
format(long = true) {}

/**
Expand Down
185 changes: 76 additions & 109 deletions parser/parsedHedTag.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
import { generateIssue, IssueError } from '../common/issues/issues'
import { Schema } from '../common/schema/types'
import { getTagLevels, replaceTagNameWithPound } from '../utils/hedStrings'
import { IssueError } from '../common/issues/issues'
import { getTagLevels } from '../utils/hedStrings'
import ParsedHedSubstring from './parsedHedSubstring'
import { SchemaValueTag } from '../validator/schema/types'
import { SchemaValueTag } from '../schema/entries'
import TagConverter from './tagConverter'
import { Schema } from '../schema/containers'

/**
* A parsed HED tag.
*/
export class ParsedHedTag extends ParsedHedSubstring {
export default class ParsedHedTag extends ParsedHedSubstring {
/**
* The formatted canonical version of the HED tag.
* @type {string}
Expand All @@ -24,33 +24,65 @@ export class ParsedHedTag extends ParsedHedSubstring {
* @type {Schema}
*/
schema
/**
* The schema's representation of this tag.
*
* @type {SchemaTag}
* @private
*/
_schemaTag
/**
* The remaining part of the tag after the portion actually in the schema.
*
* @type {string}
* @private
*/
_remainder

/**
* Constructor.
*
* @param {string} originalTag The original HED tag.
* @param {number[]} originalBounds The bounds of the HED tag in the original HED string.
* @param {TagSpec} tagSpec The token for this tag.
* @param {Schemas} hedSchemas The collection of HED schemas.
* @param {string} hedString The original HED string.
* @throws {IssueError} If tag conversion or parsing fails.
*/
constructor(originalTag, originalBounds) {
super(originalTag, originalBounds)
constructor(tagSpec, hedSchemas, hedString) {
super(tagSpec.tag, tagSpec.bounds)

this.canonicalTag = this.originalTag
this._convertTag(hedSchemas, hedString, tagSpec)

this.formattedTag = this._formatTag()
}

/**
* Override of {@link Object.prototype.toString}.
* Convert this tag to long form.
*
* @returns {string} The original form of this HED tag.
* @param {Schemas} hedSchemas The collection of HED schemas.
* @param {string} hedString The original HED string.
* @param {TagSpec} tagSpec The token for this tag.
* @throws {IssueError} If tag conversion or parsing fails.
*/
toString() {
if (this.schema?.prefix) {
return this.schema.prefix + ':' + this.originalTag
} else {
return this.originalTag
_convertTag(hedSchemas, hedString, tagSpec) {
const schemaName = tagSpec.library
this.schema = hedSchemas.getSchema(schemaName)
if (this.schema === undefined) {
if (schemaName !== '') {
IssueError.generateAndThrow('unmatchedLibrarySchema', {
tag: this.originalTag,
library: schemaName,
})
} else {
IssueError.generateAndThrow('unmatchedBaseSchema', {
tag: this.originalTag,
})
}
}

const [schemaTag, remainder] = new TagConverter(tagSpec, hedSchemas).convert()
this._schemaTag = schemaTag
this._remainder = remainder
this.canonicalTag = this._schemaTag.longExtend(remainder)
}

/**
Expand All @@ -59,9 +91,34 @@ export class ParsedHedTag extends ParsedHedSubstring {
* @param {boolean} long Whether the tags should be in long form.
* @returns {string} The nicely formatted version of this tag.
*/
// eslint-disable-next-line no-unused-vars
format(long = true) {
return this.toString()
let tagName
if (long) {
tagName = this._schemaTag?.longExtend(this._remainder)
} else {
tagName = this._schemaTag?.extend(this._remainder)
}
if (tagName === undefined) {
tagName = this.originalTag
}
if (this.schema?.prefix) {
return this.schema.prefix + ':' + tagName
} else {
return tagName
}
}

/**
* Override of {@link Object.prototype.toString}.
*
* @returns {string} The original form of this HED tag.
*/
toString() {
if (this.schema?.prefix) {
return this.schema.prefix + ':' + this.originalTag
} else {
return this.originalTag
}
}

/**
Expand Down Expand Up @@ -263,96 +320,6 @@ export class ParsedHedTag extends ParsedHedSubstring {
equivalent(other) {
return other instanceof ParsedHedTag && this.formattedTag === other.formattedTag && this.schema === other.schema
}
}

/**
* A parsed HED-3G tag.
*/
export class ParsedHed3Tag extends ParsedHedTag {
/**
* The schema's representation of this tag.
*
* @type {SchemaTag}
* @private
*/
_schemaTag
/**
* The remaining part of the tag after the portion actually in the schema.
*
* @type {string}
* @private
*/
_remainder

/**
* Constructor.
*
* @param {TagSpec} tagSpec The token for this tag.
* @param {Schemas} hedSchemas The collection of HED schemas.
* @param {string} hedString The original HED string.
* @throws {IssueError} If tag conversion or parsing fails.
*/
constructor(tagSpec, hedSchemas, hedString) {
super(tagSpec.tag, tagSpec.bounds)

this._convertTag(hedSchemas, hedString, tagSpec)

this.formattedTag = this._formatTag()
}

/**
* Convert this tag to long form.
*
* @param {Schemas} hedSchemas The collection of HED schemas.
* @param {string} hedString The original HED string.
* @param {TagSpec} tagSpec The token for this tag.
* @throws {IssueError} If tag conversion or parsing fails.
*/
_convertTag(hedSchemas, hedString, tagSpec) {
const schemaName = tagSpec.library
this.schema = hedSchemas.getSchema(schemaName)
if (this.schema === undefined) {
this.canonicalTag = this.originalTag
if (schemaName !== '') {
IssueError.generateAndThrow('unmatchedLibrarySchema', {
tag: this.originalTag,
library: schemaName,
})
} else {
IssueError.generateAndThrow('unmatchedBaseSchema', {
tag: this.originalTag,
})
}
}

const [schemaTag, remainder] = new TagConverter(tagSpec, hedSchemas).convert()
this._schemaTag = schemaTag
this._remainder = remainder
this.canonicalTag = this._schemaTag.longExtend(remainder)
}

/**
* Nicely format this tag.
*
* @param {boolean} long Whether the tags should be in long form.
* @returns {string} The nicely formatted version of this tag.
*/
format(long = true) {
let tagName
if (long) {
tagName = this._schemaTag?.longExtend(this._remainder)
} else {
tagName = this._schemaTag?.extend(this._remainder)
}
if (tagName === undefined) {
tagName = this.originalTag
}
if (this.schema?.prefix) {
return this.schema.prefix + ':' + tagName
} else {
return tagName
}
}

/**
* Determine if this HED tag is in the linked schema.
Expand Down
6 changes: 0 additions & 6 deletions parser/parser.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,6 @@
import { mergeParsingIssues } from '../utils/hedData'
import { generateIssue } from '../common/issues/issues'
import ParsedHedString from './parsedHedString'
import HedStringSplitter from './splitter'
import { getCharacterCount, stringIsEmpty } from '../utils/string'

const openingGroupCharacter = '('
const closingGroupCharacter = ')'
const delimiters = new Set([','])

/**
* A parser for HED strings.
Expand Down
Loading

0 comments on commit 28e9d7a

Please sign in to comment.