Skip to content

Commit

Permalink
Merge pull request #1796 from kujirahand/fix_prop_syntax
Browse files Browse the repository at this point in the history
Fix $prop syntax
  • Loading branch information
kujirahand authored Nov 16, 2024
2 parents ea6f683 + 39e2c2d commit cebed6e
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 35 deletions.
6 changes: 1 addition & 5 deletions core/src/nako_gen.mts
Original file line number Diff line number Diff line change
Expand Up @@ -1856,11 +1856,7 @@ export class NakoGen {
let code = '/*[convLetProp]*/'
// 変数が存在しないとき
if (res === null) {
throw NakoSyntaxError.fromNode('変数が見当たりません。', node)
}
// ネームスペースを削除
if (prop.indexOf('__') >= 0) {
prop = prop.split('__')[1]
throw NakoSyntaxError.fromNode(`変数『${name}』が見当たりません。`, node)
}
// プロパティへの代入式を作る
const propVar = `${res.js}['${prop}']`
Expand Down
46 changes: 17 additions & 29 deletions core/src/nako_parser3.mts
Original file line number Diff line number Diff line change
Expand Up @@ -1556,36 +1556,24 @@ export class NakoParser extends NakoParserBase {
throw NakoSyntaxError.fromNode(`${this.nodeToStr(word, { depth: 1 }, false)}への代入文で計算式に以下の書き間違いがあります。\n${err.message}`, map)
}
}
// プロパティ代入文
if (this.check2(['word', '$', 'word', 'eq'])) {
// プロパティ代入文 (#1793)
if (this.check2(['word', '$', 'word', 'eq']) || this.check2(['word', '$', 'string', 'eq'])) {
const word = this.peek()
let threw = false
try {
if (this.accept(['word', '$', 'word', 'eq', this.yCalc])) {
const nameToken = this.getVarName(this.y[0])
const propToken = this.getVarName(this.y[2])
const valueToken = this.y[4]
return {
type: 'let_prop',
name: (nameToken as AstStrValue).value,
index: [propToken],
blocks: [valueToken],
josi: '',
...map,
end: this.peekSourceMap()
} as AstLet
} else {
threw = true
this.logger.debug(`${this.nodeToStr(word, { depth: 1 }, true)}への代入文で計算式に書き間違いがあります。`, word)
throw NakoSyntaxError.fromNode(`${this.nodeToStr(word, { depth: 1 }, false)}への代入文で計算式に書き間違いがあります。`, map)
}
} catch (err: any) {
if (threw) {
throw err
}
this.logger.debug(`${this.nodeToStr(word, { depth: 1 }, true)}への代入文で計算式に以下の書き間違いがあります。\n${err.message}`, word)
throw NakoSyntaxError.fromNode(`${this.nodeToStr(word, { depth: 1 }, false)}への代入文で計算式に以下の書き間違いがあります。\n${err.message}`, map)
if (this.accept(['word', '$', 'word', 'eq', this.yCalc]) || this.accept(['word', '$', 'string', 'eq', this.yCalc])) {
const nameToken = this.getVarName(this.y[0])
const propToken = this.y[2]
const valueToken = this.y[4]
return {
type: 'let_prop',
name: (nameToken as AstStrValue).value,
index: [propToken],
blocks: [valueToken],
josi: '',
...map,
end: this.peekSourceMap()
} as AstLet
}
throw NakoSyntaxError.fromNode(`${this.nodeToStr(word, { depth: 1 }, false)}への代入文の計算式に書き間違いがあります。`, map)
}

// let_array ?
Expand Down Expand Up @@ -2324,7 +2312,7 @@ export class NakoParser extends NakoParserBase {
}

// word$prop
if (word.josi === '' && this.check2(['$', 'word'])) {
if (word.josi === '' && (this.check2(['$', 'word']) || this.check2(['$', 'string']))) {
this.get() // skip '$'
const prop = this.get() as Token
return {
Expand Down
4 changes: 4 additions & 0 deletions core/test/basic_test.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -390,4 +390,8 @@ describe('basic', async () => {
'A={"幅": 3, "__setProp": F_SET, "__getProp": F_GET};\n' +
'A$幅=5; A$幅を表示', '50')
})
it('オブジェクトを手軽に設定する-文字列 (#1793)', async () => {
await cmp('A={"幅":30};A$"幅"=50;A$"幅"を表示', '50')
await cmp('A={"高":30};A$"高"=50;A$"高"を表示', '50') // 送り仮名の省略
})
})
2 changes: 1 addition & 1 deletion src/plugin_browser_dom_parts.mts
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ export default {
const waAttr = sys.__getSysVar('DOM和属性')
if (waAttr[prop] !== undefined) {
prop = waAttr[prop]
btn.setAttribute(prop, value)
btn[prop] = value
return
}
// others
Expand Down

0 comments on commit cebed6e

Please sign in to comment.