-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #75 from rycont/fix-similar-function-header-parsin…
…g-error Fix similar function header parsing error
- Loading branch information
Showing
19 changed files
with
236 additions
and
159 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,5 +6,5 @@ | |
}, | ||
"name": "@dalbit-yaksok/core", | ||
"exports": "./mod.ts", | ||
"version": "0.2.0-RC.7" | ||
"version": "0.2.0-RC.8" | ||
} |
33 changes: 33 additions & 0 deletions
33
core/prepare/parse/dynamicRule/functions/declare-rule/common.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
import { Expression, Identifier } from '../../../../../node/base.ts' | ||
import { type Token, TOKEN_TYPE } from '../../../../tokenize/token.ts' | ||
import type { PatternUnit } from '../../../type.ts' | ||
|
||
export function functionHeaderToPattern(tokens: Token[]): PatternUnit[] { | ||
const units = tokens.map(mapTokenToPatternUnit).filter(Boolean) | ||
return units as NonNullable<(typeof units)[number]>[] | ||
} | ||
|
||
function mapTokenToPatternUnit(token: Token): PatternUnit | null { | ||
if (token.type === TOKEN_TYPE.IDENTIFIER) { | ||
return { | ||
type: Identifier, | ||
value: token.value, | ||
} | ||
} | ||
|
||
if (token.type === TOKEN_TYPE.OPENING_PARENTHESIS) { | ||
return { | ||
type: Expression, | ||
value: '(', | ||
} | ||
} | ||
|
||
if (token.type === TOKEN_TYPE.CLOSING_PARENTHESIS) { | ||
return { | ||
type: Expression, | ||
value: ')', | ||
} | ||
} | ||
|
||
return null | ||
} |
63 changes: 63 additions & 0 deletions
63
core/prepare/parse/dynamicRule/functions/declare-rule/ffi-declare-rule.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
import { Evaluable, Expression, Identifier } from '../../../../../node/base.ts' | ||
import { DeclareFFI, FFIBody } from '../../../../../node/ffi.ts' | ||
import { EOL } from '../../../../../node/misc.ts' | ||
import { Token } from '../../../../tokenize/token.ts' | ||
import { PatternUnit, Rule } from '../../../type.ts' | ||
import { functionHeaderToPattern } from './common.ts' | ||
|
||
const PREFIX: PatternUnit[] = [ | ||
{ | ||
type: Identifier, | ||
value: '번역', | ||
}, | ||
{ | ||
type: Expression, | ||
value: '(', | ||
}, | ||
{ | ||
type: Evaluable, | ||
}, | ||
{ | ||
type: Expression, | ||
value: ')', | ||
}, | ||
{ | ||
type: Expression, | ||
value: ',', | ||
}, | ||
] | ||
|
||
const SUFFIX: PatternUnit[] = [ | ||
{ | ||
type: EOL, | ||
}, | ||
{ | ||
type: FFIBody, | ||
}, | ||
] | ||
|
||
export function tokensToFFIDeclareRule(headerTokens: Token[]): Rule { | ||
const headerPattern = functionHeaderToPattern(headerTokens) | ||
const pattern = [...PREFIX, ...headerPattern, ...SUFFIX] | ||
|
||
return { | ||
pattern, | ||
factory: (nodes, matchedTokens) => { | ||
const runtime = (nodes[2] as Identifier).value | ||
const body = (nodes[nodes.length - 1] as FFIBody).code | ||
const name = headerTokens | ||
.map((token) => token.value) | ||
.join('') | ||
.trim() | ||
|
||
return new DeclareFFI( | ||
{ | ||
name, | ||
runtime, | ||
body, | ||
}, | ||
matchedTokens, | ||
) | ||
}, | ||
} | ||
} |
52 changes: 52 additions & 0 deletions
52
core/prepare/parse/dynamicRule/functions/declare-rule/yaksok-declare-rule.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
import { Expression, Identifier } from '../../../../../node/base.ts' | ||
import { Block } from '../../../../../node/block.ts' | ||
import { DeclareFunction } from '../../../../../node/function.ts' | ||
import { EOL } from '../../../../../node/misc.ts' | ||
import { Token } from '../../../../tokenize/token.ts' | ||
import { PatternUnit, Rule } from '../../../type.ts' | ||
import { functionHeaderToPattern } from './common.ts' | ||
|
||
const PREFIX: PatternUnit[] = [ | ||
{ | ||
type: Identifier, | ||
value: '약속', | ||
}, | ||
{ | ||
type: Expression, | ||
value: ',', | ||
}, | ||
] | ||
|
||
const SUFFIX: PatternUnit[] = [ | ||
{ | ||
type: EOL, | ||
}, | ||
{ | ||
type: Block, | ||
}, | ||
] | ||
|
||
export function tokensToYaksokDeclareRule(headerTokens: Token[]): Rule { | ||
const headerPattern = functionHeaderToPattern(headerTokens) | ||
const pattern = [...PREFIX, ...headerPattern, ...SUFFIX] | ||
|
||
return { | ||
pattern, | ||
factory: (nodes, matchedNodes) => { | ||
const body = nodes[nodes.length - 1] as Block | ||
|
||
const name = headerTokens | ||
.map((token) => token.value) | ||
.join('') | ||
.trim() | ||
|
||
return new DeclareFunction( | ||
{ | ||
body, | ||
name, | ||
}, | ||
matchedNodes, | ||
) | ||
}, | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,33 +1,35 @@ | ||
import { getFunctionTemplatesFromTokens } from './get-function-templates.ts' | ||
import { convertTokensToFunctionTemplate } from './get-function-templates.ts' | ||
import { tokensToFFIDeclareRule } from './declare-rule/ffi-declare-rule.ts' | ||
import { createFunctionInvokeRule } from './invoke-rule.ts' | ||
import { getFunctionDeclareRanges } from '../../../../util/get-function-declare-ranges.ts' | ||
|
||
import { getFunctionDeclareRanges } from '../../../../util/get-function-declare-ranges.ts' | ||
import type { Token } from '../../../tokenize/token.ts' | ||
import { tokensToYaksokDeclareRule } from './declare-rule/yaksok-declare-rule.ts' | ||
|
||
export function createLocalDynamicRules( | ||
tokens: Token[], | ||
functionDeclareRanges: [number, number][] = getFunctionDeclareRanges( | ||
tokens, | ||
), | ||
functionDeclareRanges = getFunctionDeclareRanges(tokens), | ||
) { | ||
const yaksokTemplates = getFunctionTemplatesFromTokens( | ||
tokens, | ||
functionDeclareRanges, | ||
'yaksok', | ||
) | ||
const getTokensFromRange = getTokensFromRangeFactory(tokens) | ||
|
||
const ffiTemplates = getFunctionTemplatesFromTokens( | ||
tokens, | ||
functionDeclareRanges, | ||
'ffi', | ||
) | ||
const yaksokHeaders = functionDeclareRanges.yaksok.map(getTokensFromRange) | ||
const ffiHeaders = functionDeclareRanges.ffi.map(getTokensFromRange) | ||
|
||
const invokingRules = [...yaksokHeaders, ...ffiHeaders] | ||
.map(convertTokensToFunctionTemplate) | ||
.flatMap(createFunctionInvokeRule) | ||
.toSorted((a, b) => b.pattern.length - a.pattern.length) | ||
|
||
const yaksokInvokeRules = yaksokTemplates.flatMap(createFunctionInvokeRule) | ||
const ffiInvokeRules = ffiTemplates.flatMap(createFunctionInvokeRule) | ||
const ffiDeclareRules = ffiHeaders.map(tokensToFFIDeclareRule) | ||
|
||
const allRules = [...yaksokInvokeRules, ...ffiInvokeRules].toSorted( | ||
const yaksokDeclareRules = yaksokHeaders.map(tokensToYaksokDeclareRule) | ||
const declareRules = [...ffiDeclareRules, ...yaksokDeclareRules].toSorted( | ||
(a, b) => b.pattern.length - a.pattern.length, | ||
) | ||
|
||
return allRules | ||
return [declareRules, invokingRules] | ||
} | ||
|
||
const getTokensFromRangeFactory = | ||
(tokens: Token[]) => (range: [number, number]) => | ||
tokens.slice(range[0], range[1]) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.