
# Nyash Unified Grammar v1.1 - Phase 11.9 AI協働革命対応版
version = "1.1"
language = "nyash"

# =============================================================================
# Core Keywords - 自己参照・デリゲーション・制御フロー
# =============================================================================

[keywords.me]
token = "ME"
category = "object_reference"
semantic = "current_instance"
syntax = "me.<field>"
example = "me.name = value"
deprecated_aliases = ["this", "self", "@"]
ai_hint = "Always use 'me' for self-reference, never 'this'"

[keywords.from]
token = "FROM"
category = "delegation"
semantic = "parent_method_call"
syntax = "from <parent>.<method>(<args>)"
example = "from Animal.birth(name)"
deprecated_aliases = ["super", "parent", "base"]
ai_hint = "Always use 'from' for parent calls, never 'super'"

[keywords.loop]
token = "LOOP"
category = "control_flow"
semantic = "conditional_iteration"
syntax = "loop(<condition>) { <body> }"
example = "loop(i < 10) { i = i + 1 }"
deprecated_aliases = ["while", "for"]
ai_hint = "Only 'loop' for iteration, never 'while' or 'for'"

[keywords.box]
token = "BOX"
category = "declaration"
semantic = "class_declaration"
syntax = "box <name> from <parent>? { <body> }"
example = "box Cat from Animal { }"
deprecated_aliases = ["class", "struct", "type"]
ai_hint = "Use 'box' for all class definitions"

[keywords.local]
token = "LOCAL"
category = "declaration"
semantic = "variable_declaration"
syntax = "local <name>"
example = "local temp"
deprecated_aliases = ["var", "let", "const"]
ai_hint = "Use 'local' for variable declarations"

[keywords.peek]
token = "PEEK"
category = "pattern_matching"
semantic = "pattern_match_expression"
syntax = "peek <expr> { <pattern> => <value>, ... }"
example = 'peek status { "ok" => 200, "error" => 500, else => 404 }'
deprecated_aliases = ["match", "switch", "case"]
ai_hint = "Use 'peek' for pattern matching, never if-else chains"

# =============================================================================
# AI Training Section - ChatGPT「恐ろしいコード」防止対策
# =============================================================================

[[ai_training.correct_patterns]]
pattern = "peek ch { \"0\" => 0, \"1\" => 1, \"2\" => 2, else => 0 }"
category = "pattern_matching"
description = "Use peek for character-to-digit conversion"
use_case = ["parsing", "tokenization", "state_transitions"]

[[ai_training.correct_patterns]]
pattern = "loop(condition) { }"
category = "iteration"
description = "Standard loop construct"
use_case = ["iteration", "while_loops", "counting"]

[[ai_training.correct_patterns]]
pattern = "me.field = value"
category = "assignment"
description = "Self-reference assignment"
use_case = ["object_field_access", "instance_modification"]

[[ai_training.correct_patterns]]
pattern = "from Parent.method(args)"
category = "delegation"
description = "Parent method delegation"
use_case = ["inheritance", "parent_calls", "super_calls"]

# =============================================================================
# Common Mistakes - AI向け誤り修正データベース
# =============================================================================

[[ai_training.common_mistakes]]
mistake = 'if ch == "0" { d = 0 } else if ch == "1" { d = 1 } else if ch == "2" { d = 2 }'
correction = 'd = peek ch { "0" => 0, "1" => 1, "2" => 2, else => 0 }'
severity = "error"
reason = "Use peek expression instead of if-else chains for pattern matching"
context = "digit_parsing"

[[ai_training.common_mistakes]]
mistake = "while(condition) { }"
correction = "loop(condition) { }"
severity = "error" 
reason = "Nyash uses 'loop' keyword, not 'while'"
context = "iteration"

[[ai_training.common_mistakes]]
mistake = "this.value"
correction = "me.value"
severity = "error"
reason = "Use 'me' for self-reference, not 'this'"
context = "object_reference"

[[ai_training.common_mistakes]]
mistake = "super.init()"
correction = "from Parent.init()"
severity = "error"
reason = "Use 'from Parent.method()' for delegation, not 'super'"
context = "inheritance"

[[ai_training.common_mistakes]]
mistake = "for i in array { }"
correction = "loop(i < array.length()) { /* use array[i] */ }"
severity = "error"
reason = "Nyash doesn't have for-in loops, use indexed loop"
context = "iteration"

[[ai_training.common_mistakes]]
mistake = "var x = 5;"
correction = "local x; x = 5"
severity = "error"
reason = "Use 'local' for declarations, no semicolons"
context = "variable_declaration"

# =============================================================================
# Operators Configuration
# =============================================================================

[operators.add]
symbol = "+"
coercion = "string_priority"
rules = [
    ["String", "String", "String", "concat"],
    ["String", "Integer", "String", "concat"], 
    ["Integer", "String", "String", "concat"],
    ["String", "Bool", "String", "concat"],
    ["Bool", "String", "String", "concat"],
    ["String", "Other", "String", "concat"],
    ["Other", "String", "String", "concat"],
    ["Integer", "Integer", "Integer", "add_i64"],
    ["Float", "Float", "Float", "add_f64"],
]

[operators.sub]
symbol = "-"
coercion = "numeric_only"
rules = [
    ["Integer", "Integer", "Integer", "sub_i64"],
    ["Float", "Float", "Float", "sub_f64"],
]

[operators.mul]
symbol = "*"
coercion = "numeric_only"
rules = [
    ["Integer", "Integer", "Integer", "mul_i64"],
    ["Float", "Float", "Float", "mul_f64"],
]

[operators.div]
symbol = "/"
coercion = "numeric_only"
rules = [
    ["Integer", "Integer", "Integer", "div_i64"],
    ["Float", "Float", "Float", "div_f64"],
]

# =============================================================================
# Syntax Rules - 構文制約と検証ルール
# =============================================================================

[syntax.statements]
allow = [
  "box", "global", "function", "static",
  "if", "loop", "break", "return", "print",
  "nowait", "include", "local", "outbox", "try", "throw", "using", "from",
  "peek"  # Phase 11.9で追加
]

[syntax.expressions]
allow_binops = ["add", "sub", "mul", "div", "and", "or", "eq", "ne"]

# Box定義の制約
[syntax.box_definition]
pattern = "box <identifier> (from <identifier_list>)? { <box_body> }"

[[syntax.box_definition.constraints]]
name = "init_comma_required"
rule = "init block fields must be comma-separated"
valid = "birth(name, age) { }"
invalid = "birth(name age) { }"

[[syntax.box_definition.constraints]]
name = "constructor_exclusive"
rule = "Only one of birth/pack can be defined"
valid = "birth() { }"
invalid = "birth() { } pack() { }"

# Delegation呼び出しの制約
[syntax.delegation_call]
pattern = "from <identifier>.<identifier>(<expression_list>?)"

[[syntax.delegation_call.constraints]]
name = "parent_must_exist"
rule = "Parent must be declared in 'from' clause"

[[syntax.delegation_call.constraints]]
name = "method_resolution"
rule = "Method lookup follows delegation chain"

# =============================================================================
# Semantic Rules - 意味論的制約
# =============================================================================

[semantic.variable_declaration]
rule = "Variables must be declared before use"
scope = "function"
warning_undeclared = "Use 'local' for clarity"

[semantic.method_resolution]
order = ["current_instance", "delegated_parents", "error_not_found"]

# =============================================================================
# AI Export Configuration - ChatGPT向けエクスポート設定
# =============================================================================

[ai_export]
include_deprecated = false
include_examples = true
include_context = true
max_pattern_length = 200

[ai_export.formats]
json = true
prompt_txt = true
training_jsonl = true
