Refactor if handling
This commit is contained in:
@@ -997,7 +997,7 @@ const legacyBlockMatcher = (p1:string,matcherArg:matcherArg) => {
|
||||
return null
|
||||
}
|
||||
|
||||
type blockMatch = 'ignore'|'parse'|'nothing'|'parse-pure'
|
||||
type blockMatch = 'ignore'|'parse'|'nothing'|'parse-pure'|'pure'|'each'
|
||||
|
||||
|
||||
function blockStartMatcher(p1:string,matcherArg:matcherArg):blockMatch{
|
||||
@@ -1009,6 +1009,9 @@ function blockStartMatcher(p1:string,matcherArg:matcherArg):blockMatch{
|
||||
}
|
||||
return 'ignore'
|
||||
}
|
||||
if(p1 === '#pure'){
|
||||
return 'pure'
|
||||
}
|
||||
return 'nothing'
|
||||
}
|
||||
|
||||
@@ -1017,6 +1020,9 @@ function blockEndMatcher(p1:string,type:blockMatch,matcherArg:matcherArg):string
|
||||
case 'ignore':{
|
||||
return ''
|
||||
}
|
||||
case 'pure':{
|
||||
return p1
|
||||
}
|
||||
case 'parse':{
|
||||
const trimedLines = p1.split('\n').map((v) => {
|
||||
return v.trim()
|
||||
@@ -1073,9 +1079,9 @@ export function risuChatParser(da:string, arg:{
|
||||
let pointer = 0;
|
||||
let nested:string[] = [""]
|
||||
let stackType = new Uint8Array(512)
|
||||
let pureMode = false
|
||||
let pureModeType:''|'pureSyntax'|'block' = ''
|
||||
let blockType:blockMatch = 'nothing'
|
||||
let pureModeNest:Map<number,boolean> = new Map()
|
||||
let pureModeNestType:Map<number,string> = new Map()
|
||||
let blockNestType:Map<number,blockMatch> = new Map()
|
||||
let commentMode = false
|
||||
let commentLatest:string[] = [""]
|
||||
let commentV = new Uint8Array(512)
|
||||
@@ -1091,7 +1097,17 @@ export function risuChatParser(da:string, arg:{
|
||||
role: arg.role,
|
||||
runVar: arg.runVar ?? false,
|
||||
}
|
||||
let pef = performance.now()
|
||||
|
||||
|
||||
const isPureMode = () => {
|
||||
return pureModeNest.size > 0
|
||||
}
|
||||
const pureModeType = () => {
|
||||
if(pureModeNest.size === 0){
|
||||
return ''
|
||||
}
|
||||
return pureModeNestType.get(nested.length) ?? [...pureModeNestType.values()].at(-1) ?? ''
|
||||
}
|
||||
while(pointer < da.length){
|
||||
switch(da[pointer]){
|
||||
case '{':{
|
||||
@@ -1129,7 +1145,7 @@ export function risuChatParser(da:string, arg:{
|
||||
pointer++
|
||||
const dat = nested.shift()
|
||||
if(dat.startsWith('#')){
|
||||
if(pureMode){
|
||||
if(isPureMode()){
|
||||
nested[0] += `{{${dat}}}`
|
||||
}
|
||||
const matchResult = blockStartMatcher(dat, matcherObj)
|
||||
@@ -1138,33 +1154,34 @@ export function risuChatParser(da:string, arg:{
|
||||
break
|
||||
}
|
||||
else{
|
||||
if(matchResult !== 'parse'){
|
||||
pureMode = true
|
||||
pureModeType = 'block'
|
||||
}
|
||||
blockType = matchResult
|
||||
nested.unshift('')
|
||||
stackType[nested.length] = 5
|
||||
blockNestType.set(nested.length, matchResult)
|
||||
if(matchResult === 'ignore' || matchResult === 'pure'){
|
||||
pureModeNest.set(nested.length, true)
|
||||
pureModeNestType.set(nested.length, "block")
|
||||
}
|
||||
break
|
||||
}
|
||||
}
|
||||
if(dat.startsWith('/')){
|
||||
if(stackType[nested.length] === 5){
|
||||
const dat2 = nested.shift()
|
||||
if(pureMode && pureModeType === 'block'){
|
||||
pureMode = false
|
||||
pureModeType = ''
|
||||
const blockType = blockNestType.get(nested.length)
|
||||
if(blockType === 'ignore' || blockType === 'pure'){
|
||||
pureModeNest.delete(nested.length)
|
||||
pureModeNestType.delete(nested.length)
|
||||
}
|
||||
blockNestType.delete(nested.length)
|
||||
const dat2 = nested.shift()
|
||||
const matchResult = blockEndMatcher(dat2.trim(), blockType, matcherObj)
|
||||
if(matchResult === ''){
|
||||
break
|
||||
}
|
||||
nested[0] += matchResult
|
||||
blockType = 'nothing'
|
||||
break
|
||||
}
|
||||
}
|
||||
const mc = (pureMode) ? null :matcher(dat, matcherObj)
|
||||
const mc = isPureMode() ? null :matcher(dat, matcherObj)
|
||||
nested[0] += mc ?? `{{${dat}}}`
|
||||
break
|
||||
}
|
||||
@@ -1173,26 +1190,11 @@ export function risuChatParser(da:string, arg:{
|
||||
break
|
||||
}
|
||||
const dat = nested.shift()
|
||||
if(pureMode && pureModeType !== 'pureSyntax' && pureModeType !== ''){
|
||||
if(isPureMode() && pureModeType() !== 'pureSyntax' && pureModeType() !== ''){
|
||||
nested[0] += `<${dat}>`
|
||||
break
|
||||
}
|
||||
switch(dat){
|
||||
case 'Pure':{
|
||||
pureMode = true
|
||||
pureModeType = 'pureSyntax'
|
||||
break
|
||||
}
|
||||
case '/Pure':{
|
||||
if(pureModeType === 'pureSyntax'){
|
||||
pureMode = false
|
||||
pureModeType = ''
|
||||
}
|
||||
else{
|
||||
nested[0] += `<${dat}>`
|
||||
break
|
||||
}
|
||||
}
|
||||
case 'Comment':{
|
||||
if(arg.runVar){
|
||||
break
|
||||
@@ -1245,7 +1247,7 @@ export function risuChatParser(da:string, arg:{
|
||||
break
|
||||
}
|
||||
default:{
|
||||
const mc = (pureMode) ? null : smMatcher(dat, matcherObj)
|
||||
const mc = isPureMode() ? null : smMatcher(dat, matcherObj)
|
||||
nested[0] += mc ?? `<${dat}>`
|
||||
break
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user