Skip to content

Commit

Permalink
特別名前トークン ${xxx} を追加
Browse files Browse the repository at this point in the history
  • Loading branch information
kujirahand committed Nov 24, 2024
1 parent 757781b commit da24b2b
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 0 deletions.
31 changes: 31 additions & 0 deletions core/src/nako_lex_rules.mts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
* なでしこ3字句解析のためのルール
*/

import { NakoLexerError } from './nako_errors.mjs'
import { josiRE, removeJosiMap } from './nako_josi_list.mjs'
import { TokenType } from './nako_token.mjs'

Expand Down Expand Up @@ -104,6 +105,7 @@ export const rules: NakoLexRule[] = [
{ name: ')', pattern: /^\)/, readJosi: true },
{ name: '|', pattern: /^\|/ },
{ name: '??', pattern: /^\?\?/ }, // 「表示」のエイリアス #1745
{ name: 'word', pattern: /^\$\{.+?\}/, cbParser: src => cbExtWord(src) }, // 特別名前トークン(#1836)(#672)
{ name: '$', pattern: /^(\$|\.)/ }, // プロパティアクセス (#1793)(#1807)
{ name: 'string', pattern: /^🌿/, cbParser: src => cbString('🌿', '🌿', src) },
{ name: 'string_ex', pattern: /^🌴/, cbParser: src => cbString('🌴', '🌴', src) },
Expand Down Expand Up @@ -287,6 +289,35 @@ function cbString (beginTag: string, closeTag: string, src: string): NakoLexPars
return { src, res, josi, numEOL }
}

function cbExtWord (src: string): NakoLexParseResult {
let res = ''
let josi = ''
let numEOL = 0

src = src.substring(2) // skip '${'
const i = src.indexOf('}')
if (i < 0) { // not found
throw new Error('変数名の終わりが見つかりません。')
}
res = src.substring(0, i)
src = src.substring(i + 1)

// 文字列直後の助詞を取得
const j = josiRE.exec(src)
if (j) {
josi = j[0].replace(/^\s+/, '')
src = src.substring(j[0].length)
// 助詞の後のカンマ #877
if (src.charAt(0) === ',') { src = src.substring(1) }
}

// 改行を数える(あり得ないけど)
for (let i = 0; i < res.length; i++) { if (res.charAt(i) === '\n') { numEOL++ } }
if (numEOL > 0) { throw new Error('変数名に改行を含めることはできません。') }

return { src, res, josi, numEOL }
}

function parseNumber (n: string): number {
return Number(n.replace(/_/g, ''))
}
4 changes: 4 additions & 0 deletions core/test/basic_test.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -407,4 +407,8 @@ describe('basic', async () => {
await cmp('A=30em;Aを表示', '30em')
await cmp('A=30px;AをJSONエンコードして表示', '"30px"')
})
it('特別名前トークンのテスト #672 #1836', async () => {
await cmp('《リンゴの値段》=500;《リンゴの値段》を表示', '500') // #672 大なり記号ではなく、二重カッコであることに注意
await cmp('${リンゴの値段}=500;${リンゴの値段}を表示', '500') // #1836
})
})

0 comments on commit da24b2b

Please sign in to comment.