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