|
@@ -104,20 +104,23 @@ var TuringMachine = function(obj){ |
|
|
var openpIdx = unescapedIndexOf(input, '('); |
|
|
var commaIdx; |
|
|
var closeIdx; |
|
|
var commaError; |
|
|
if (currtype === 4) { // parsing movement, it's the last part of the 5-uple |
|
|
commaIdx = unescapedIndexOf(input, ')'); |
|
|
closeIdx = -1; |
|
|
commaError = 'READ_ERROR_EXPECTED_END_OF_RULE'; |
|
|
} else { |
|
|
commaIdx = unescapedIndexOf(input, ','); |
|
|
closeIdx = unescapedIndexOf(input, ')'); |
|
|
commaError = 'READ_ERROR_EXPECTED_COMMA'; |
|
|
} |
|
|
var nlineIdx = input.indexOf('\n'); |
|
|
if (openpIdx !== -1 && openpIdx < commaIdx) { |
|
|
throw new TMError('READ_ERROR_EXPECTED_COMMA'); |
|
|
throw new TMError(commaError); |
|
|
} else if (closeIdx !== -1 && closeIdx < commaIdx) { |
|
|
throw new TMError('READ_ERROR_EXPECTED_COMMA'); |
|
|
throw new TMError(commaError); |
|
|
} else if (nlineIdx !== -1 && nlineIdx < commaIdx) { |
|
|
throw new TMError('READ_ERROR_EXPECTED_COMMA'); |
|
|
throw new TMError(commaError); |
|
|
} |
|
|
var part = input.substring(0, commaIdx).replace(new RegExp('^\\s\\s*'), '').replace(new RegExp('\\s\\s*$'), ''); |
|
|
input = input.substring(commaIdx + 1); |
|
@@ -145,47 +148,47 @@ var TuringMachine = function(obj){ |
|
|
throw new TMError('READ_ERROR_WRONG_CHARACTER_CLASS'); |
|
|
} |
|
|
if (open2Idx !== -1 || clos2Idx !== -1) { |
|
|
throw new TMError('READ_ERROR_EXPECTED_COMMA'); |
|
|
throw new TMError(commaError); |
|
|
} |
|
|
if (caretIdx !== -1 && caretIdx !== open1Idx + 1) { |
|
|
throw new TMError('READ_ERROR_EXPECTED_COMMA'); |
|
|
throw new TMError(commaError); |
|
|
} |
|
|
if (currtype !== 0 && currtype !== 2 && ((open1Idx !== 0) || (clos1Idx !== part.length - 1))) { |
|
|
throw new TMError('READ_ERROR_EXPECTED_COMMA'); |
|
|
throw new TMError(commaError); |
|
|
} |
|
|
if ((currtype === 0 || currtype === 2) && unescapedIndexOf(part, '[', open1Idx + 1) !== -1) { |
|
|
throw new TMError('READ_ERROR_EXPECTED_COMMA'); |
|
|
throw new TMError(commaError); |
|
|
} |
|
|
if ((currtype === 0 || currtype === 2) && unescapedIndexOf(part, ']', clos1Idx + 1) !== -1) { |
|
|
throw new TMError('READ_ERROR_EXPECTED_COMMA'); |
|
|
throw new TMError(commaError); |
|
|
} |
|
|
return parsegroup(0, grplength, part.substring(open1Idx + 1, clos1Idx), part.substring(0, open1Idx), part.substring(clos1Idx + 1)); |
|
|
} else if (open2Idx !== -1) { |
|
|
if (clos2Idx === -1) { |
|
|
throw new TMError('READ_ERROR_WRONG_CHARACTER_CLASS'); |
|
|
} |
|
|
if (open1Idx !== -1 || clos1Idx !== -1) { |
|
|
throw new TMError('READ_ERROR_EXPECTED_COMMA'); |
|
|
throw new TMError(commaError); |
|
|
} |
|
|
if (caretIdx !== -1 && caretIdx !== open2Idx + 1) { |
|
|
throw new TMError('READ_ERROR_EXPECTED_COMMA'); |
|
|
throw new TMError(commaError); |
|
|
} |
|
|
if (currtype !== 0 && currtype !== 2 && ((open2Idx !== 0) || (clos2Idx !== part.length - 1))) { |
|
|
throw new TMError('READ_ERROR_EXPECTED_COMMA'); |
|
|
throw new TMError(commaError); |
|
|
} |
|
|
if ((currtype === 0 || currtype === 2) && unescapedIndexOf(part, '{', open2Idx + 1) !== -1) { |
|
|
throw new TMError('READ_ERROR_EXPECTED_COMMA'); |
|
|
throw new TMError(commaError); |
|
|
} |
|
|
if ((currtype === 0 || currtype === 2) && unescapedIndexOf(part, '}', clos2Idx + 1) !== -1) { |
|
|
throw new TMError('READ_ERROR_EXPECTED_COMMA'); |
|
|
throw new TMError(commaError); |
|
|
} |
|
|
return parsegroup(1, grplength, part.substring(open2Idx + 1, clos2Idx), part.substring(0, open2Idx), part.substring(clos2Idx + 1)); |
|
|
} |
|
|
if (currtype !== 0 && currtype !== 2) { // not the states |
|
|
return parsegroup((part.length > 1 ? 2 : 3), grplength, part, '', ''); |
|
|
} |
|
|
if (part.charAt(part.length - 1) === '\\' && part.charAt(part.length - 2) !== '\\') { |
|
|
throw new TMError('READ_ERROR_EXPECTED_COMMA'); |
|
|
throw new TMError(commaError); |
|
|
} |
|
|
return normalizestate(part); |
|
|
}; |
|
|