Skip to content

Commit

Permalink
Removed extra parsing of delimiters already done by tokenizer
Browse files Browse the repository at this point in the history
  • Loading branch information
VisLab committed Nov 4, 2024
1 parent 76aaf15 commit 095bc2b
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 149 deletions.
117 changes: 0 additions & 117 deletions parser/parser.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,116 +34,6 @@ class HedStringParser {
this.hedSchemas = hedSchemas
}

/**
* Check if the parentheses in a tag group match.
*
* @returns {Issue[]} Any issues found related to unmatched parentheses.
*/
_countTagGroupParentheses() {
const issues = []
const numberOfOpeningParentheses = getCharacterCount(this.hedString, openingGroupCharacter)
const numberOfClosingParentheses = getCharacterCount(this.hedString, closingGroupCharacter)

if (numberOfOpeningParentheses !== numberOfClosingParentheses) {
issues.push(
generateIssue('parentheses', {
opening: numberOfOpeningParentheses,
closing: numberOfClosingParentheses,
}),
)
}

return issues
}

/**
* Check if a comma is missing after an opening parenthesis.
*
* @param {string} lastNonEmptyCharacter The last non-empty character.
* @param {string} currentCharacter The current character in the HED string.
* @returns {boolean} Whether a comma is missing after a closing parenthesis.
*/
_isCommaMissingAfterClosingParenthesis(lastNonEmptyCharacter, currentCharacter) {
return (
lastNonEmptyCharacter === closingGroupCharacter &&
!(delimiters.has(currentCharacter) || currentCharacter === closingGroupCharacter)
)
}

/**
* Find delimiter-related issues in a HED string.
*
* @returns {Issue[]} Any issues related to delimiters.
*/
_findDelimiterIssues() {
const issues = []
let lastNonEmptyValidCharacter = ''
let lastNonEmptyValidIndex = 0
let currentTag = ''

for (let i = 0; i < this.hedString.length; i++) {
const currentCharacter = this.hedString.charAt(i)
currentTag += currentCharacter

if (stringIsEmpty(currentCharacter)) {
continue
}

if (delimiters.has(currentCharacter)) {
if (currentTag.trim() === currentCharacter) {
issues.push(
generateIssue('extraDelimiter', {
character: currentCharacter,
index: i,
string: this.hedString,
}),
)
currentTag = ''
continue
}
currentTag = ''
} else if (currentCharacter === openingGroupCharacter) {
if (currentTag.trim() !== openingGroupCharacter) {
issues.push(generateIssue('commaMissing', { tag: currentTag }))
}
currentTag = ''
} else if (this._isCommaMissingAfterClosingParenthesis(lastNonEmptyValidCharacter, currentCharacter)) {
issues.push(
generateIssue('commaMissing', {
tag: currentTag.slice(0, -1),
}),
)
break
}

lastNonEmptyValidCharacter = currentCharacter
lastNonEmptyValidIndex = i
}

if (delimiters.has(lastNonEmptyValidCharacter)) {
issues.push(
generateIssue('extraDelimiter', {
character: lastNonEmptyValidCharacter,
index: lastNonEmptyValidIndex,
string: this.hedString,
}),
)
}

return issues
}

/**
* Validate the full unparsed HED string.
*
* @returns {Object<string, Issue[]>} Any issues found during validation.
*/
_validateFullUnparsedHedString() {
const delimiterIssues = [].concat(this._countTagGroupParentheses(), this._findDelimiterIssues())

return { delimiter: delimiterIssues }
}

/**
* Parse a full HED string.
*
Expand All @@ -154,14 +44,7 @@ class HedStringParser {
return [this.hedString, {}]
}

// const fullStringIssues = this._validateFullUnparsedHedString()
// if (fullStringIssues.delimiter.length > 0) {
// fullStringIssues.syntax = []
// return [null, fullStringIssues]
// }

const [parsedTags, parsingIssues] = new HedStringSplitter(this.hedString, this.hedSchemas).splitHedString()
//const parsingIssues = Object.assign(fullStringIssues, splitIssues)
if (parsedTags === null) {
return [null, parsingIssues]
}
Expand Down
56 changes: 25 additions & 31 deletions tests/testData/tokenizerTests.data.js
Original file line number Diff line number Diff line change
Expand Up @@ -268,36 +268,6 @@ export const tokenizerTests = [
},
],
},
{
name: 'invalid-empty-tag-in-various-places',
description: 'Empty tags in various places (empty groups are allowed).',
tests: [
{
testname: 'end-in-comma',
explanation: 'Cannot end in a comma',
string: 'x,y,',
tagSpecs: [],
groupSpec: null,
errors: [generateIssue('emptyTagFound', { index: '3', string: 'x,y,' })],
},
{
testname: 'double-in-comma',
explanation: 'Cannot have double commas',
string: 'x,,y,',
tagSpecs: [],
groupSpec: null,
errors: [generateIssue('emptyTagFound', { index: '1', string: 'x,,y,' })],
},
{
testname: 'leading-comma',
string: ',x,y',
explanation: 'Cannot have a leading comma',
tagSpecs: [],
groupSpec: null,
errors: [generateIssue('emptyTagFound', { index: '0', string: ',x,y' })],
},
],
},
{
name: 'invalid-extra-slash-in-various-places',
description: 'Tags cannot have leading or trailing, or extra slashes',
Expand Down Expand Up @@ -353,9 +323,33 @@ export const tokenizerTests = [
],
},
{
name: 'invalid-comma-missing-or-extra',
name: 'invalid-commas',
description: 'Commas must separate tags and groups',
tests: [
{
testname: 'end-in-comma',
explanation: 'Cannot end in a comma',
string: 'x,y,',
tagSpecs: [],
groupSpec: null,
errors: [generateIssue('emptyTagFound', { index: '3', string: 'x,y,' })],
},
{
testname: 'double-in-comma',
explanation: 'Cannot have double commas',
string: 'x,,y,',
tagSpecs: [],
groupSpec: null,
errors: [generateIssue('emptyTagFound', { index: '1', string: 'x,,y,' })],
},
{
testname: 'leading-comma',
string: ',x,y',
explanation: 'Cannot have a leading comma',
tagSpecs: [],
groupSpec: null,
errors: [generateIssue('emptyTagFound', { index: '0', string: ',x,y' })],
},
{
testname: 'missing-comma-before-open',
explanation: 'Must have a comma before open parentheses',
Expand Down
2 changes: 1 addition & 1 deletion validator/event/init.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ const initiallyValidateHedString = function (hedString, hedSchemas, options, def
}
if (parsedString === null) {
return [null, [].concat(...Object.values(parsingIssues)), null]
} else if (parsingIssues.syntax.length + parsingIssues.delimiter.length > 0) {
} else if (parsingIssues.syntax.length > 0) {
hedSchemas = new Schemas(null)
}
let hedValidator
Expand Down

0 comments on commit 095bc2b

Please sign in to comment.