本文整理匯總了TypeScript中et-parser.Parser類的典型用法代碼示例。如果您正苦於以下問題:TypeScript Parser類的具體用法?TypeScript Parser怎麽用?TypeScript Parser使用的例子?那麽, 這裏精選的類代碼示例或許可以為您提供幫助。
在下文中一共展示了Parser類的6個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的TypeScript代碼示例。
示例1: wrap
export function wrap (source: string) {
let result = ''
wrapParser.parse(source, (state, token) => {
switch (state) {
case 'single':
result += "\\'"
break
case 'break':
result += '\\n'
break
case 'text':
result += token
break
default:
throw new Error(`parse_value_wrap_${state}`)
}
})
return `'${result}'`
}
示例2: parseAttributes
export function parseAttributes (source: string) {
let result = new AttributesResult()
parser.parse(source, (state, token) => {
switch (state) {
case 'scan':
case 'keyEnd':
case 'ignore':
break
case '_str':
case 'key':
case 'value{{':
case 'value':
case 'value\'':
case 'value"':
result.addToken(token)
break
case 'keyStart':
result.createOne()
result.state = ATTRIBUTES_STATE.Key
result.addToken(token)
break
case 'valueStart':
result.state = ATTRIBUTES_STATE.Value
break
case 'valueEnd':
case 'value\'End':
case 'value"End':
result.state = ATTRIBUTES_STATE.Scan
break
default:
throw new Error(`The state: '${state}' is not defined.`)
}
})
return result.getAttributes()
}
示例3: parseFor
export function parseFor (source: string) {
let item = '';
let index = '';
let expression = '';
let trackBy = '';
parser.parse(source, (state, token) => {
switch (state) {
case 'ignore':
case 'indexPre':
case 'indexEnd':
break;
case 'item':
item += token;
break;
case 'index':
index += token;
break;
case '_str1':
case '_str2':
case 'expression':
expression += token;
break;
case 'trackBy':
trackBy += token;
break;
default:
throw new Error(`parse_for_${state}`)
}
})
if (!index) index = '_index';
return {item, index, expression, trackBy}
}
示例4: parseValue
export function parseValue (source: string) {
let result = new ValueResult()
let value = ''
parser.parse(source, (state, token) => {
switch (state) {
case '_str1':
case '_str2':
case 'text':
case 'value':
case 'method':
case 'argument':
result.addToken(token)
break
case 'start':
result.state = VALUE_STATES.value
result.addToken('')
break
case 'pipe':
result.state = VALUE_STATES.pipe
result.createPipe()
break
case 'split':
result.splitPipe()
break
case 'end':
result.state = VALUE_STATES.text
break
case 'cancel':
result.cancel()
result.state = VALUE_STATES.value
break
default:
throw new Error(`parse_value_parse_${state}`)
}
})
if (result.state !== VALUE_STATES.text) {
result.cancel()
}
let re: IValueResult = result.format()
return re
}
示例5: parseDep
export function parseDep (source: string) {
let result = ''
let methods: string[] = []
let method = '';
parser.parse(source, function (state, token) {
switch (state) {
case '_str1':
case '_str2':
case 'text':
result += token
break;
case 'start':
case 'cancel':
result += (method + token)
method = '';
break;
case 'method':
method += token;
break;
case 'end':
result = result.substr(0, result.length - SYMBOL.length)
result += `_dep_${method}${token}`
methods.push(method.trim());
method = '';
break;
default:
throw new Error(`The state: '${state}' is not defined.`)
}
})
forEachRight(uniq(methods), (item) => {
if (item) {
result = `var _dep_${item} = _dep.${item};\n` + result
}
})
return result
}
示例6: parseOrigin
export function parseOrigin (source: string) {
let root = new Node()
let current = root
let tail = ''
parser.parse(source, (state: string, token: string, index: number) => {
switch (state) {
case '_ignore':
break
case 'text':
case '_str[':
case '_str{{':
case '_str1':
case '_str2':
case 'name':
case 'source':
case 'etName':
case 'etSource':
current.addSource(token)
break
case 'start':
current = current.createChild()
current.addSource(token)
current.state = 1
break
case 'etStart':
current = current.createChild()
current.addSource(token)
current.nodeName = '#'
current.state = NODE_STATE.NodeName
break
case 'nameEnd':
case 'etNameEnd':
current.state = NODE_STATE.Source
current.addSource(token)
break
case 'nodeEnd':
case 'etEnd':
current.state = NODE_STATE.Header
current.addSource(token)
current.state = NODE_STATE.Body
break
case 'tail':
case 'etTail':
tail += token
break
case 'tailEnd':
current = current.closeNode(tail + token)
tail = ''
break
case 'etTailEnd':
current = current.closeNode(tail + token)
tail = ''
if (current.state === NODE_STATE.Source) {
// 如果關閉之後的父節點頭還沒有讀完
// 證明這是一個從中間分離出去的
current.saveExpressions()
return 'source'
}
break
default:
throw new Error(`The state: '${state}' is not defined.`)
}
})
root.closeNode('')
return translateNode(root, true)
}