1
0
Fork 0
mirror of https://github.com/Reuh/anselme.git synced 2025-10-27 16:49:31 +00:00

[language] convert identifier to string when used as the name in a pair

This commit is contained in:
Étienne Fildadut 2024-05-17 15:12:21 +02:00
parent b47e1940e9
commit 12eb1d8f68
6 changed files with 25 additions and 6 deletions

View file

@ -4,6 +4,7 @@ local Symbol, String
local Identifier local Identifier
Identifier = ast.abstract.Node { Identifier = ast.abstract.Node {
type = "identifier", type = "identifier",
parenthesied = false, -- true if identifier was wrapped in parentheses
name = nil, name = nil,

View file

@ -3,7 +3,7 @@
local primary = require("anselme.parser.expression.primary.primary") local primary = require("anselme.parser.expression.primary.primary")
local ast = require("anselme.ast") local ast = require("anselme.ast")
local Nil = ast.Nil local Nil, Identifier = ast.Nil, ast.Identifier
local expression_to_ast = require("anselme.parser.expression.to_ast") local expression_to_ast = require("anselme.parser.expression.to_ast")
@ -27,6 +27,9 @@ return primary {
if not s then error("invalid expression inside parentheses: "..exp, 0) end if not s then error("invalid expression inside parentheses: "..exp, 0) end
rem = source:consume_leading_whitespace(opts, rem) rem = source:consume_leading_whitespace(opts, rem)
if not rem:match("^%)") then error(("unexpected %q at end of parenthesis"):format(rem:match("^[^\n]*")), 0) end if not rem:match("^%)") then error(("unexpected %q at end of parenthesis"):format(rem:match("^[^\n]*")), 0) end
if Identifier:is(exp) then
exp.parenthesied = true
end
end end
rem = source:consume(rem:match("^(%))(.*)$")) rem = source:consume(rem:match("^(%))(.*)$"))

View file

@ -2,8 +2,18 @@ local infix = require("anselme.parser.expression.secondary.infix.infix")
local operator_priority = require("anselme.common").operator_priority local operator_priority = require("anselme.common").operator_priority
local ast = require("anselme.ast")
local Call, Identifier, ArgumentTuple = ast.Call, ast.Identifier, ast.ArgumentTuple
return infix { return infix {
operator = ":", operator = ":",
identifier = "_:_", identifier = "_:_",
priority = operator_priority["_:_"] priority = operator_priority["_:_"],
build_ast = function(self, left, right)
if Identifier:is(left) and not left.parenthesied then
left = left:to_string()
end
return Call:new(Identifier:new(self.identifier), ArgumentTuple:new(left, right))
end
} }

View file

@ -415,10 +415,15 @@ The `break` and `continue` functions from the standard library also return retur
Pairs associate two arbitrary values. Pairs associate two arbitrary values.
Pairs are built using the `_:_` identifier, with the left argument giving the name and the right the value of the pair. Pairs are built using the `_:_` identifier, with the left argument giving the name and the right the value of the pair. If the left argument is an identifier, it will be converted to a string for convenience.
``` ```
"name":"value" "name":"value" -- is the same as
name:"value"
-- if you need to use the value associated with the `name` variable instead, the identifier can be wrapped in parentheses
:name = "key"
(name):"value"
``` ```
### Tuple ### Tuple

View file

@ -1,7 +1,7 @@
--# run #-- --# run #--
--- text --- --- text ---
| {}"" {}"[\"test\":1, 3:\"p\", \"test\":\"foo\", \"ho\":\"ah\", \"test\":\"test\"]" {}"" |
| {}"" {}"[\"name\":1, 3:\"p\", \"test\":\"foo\", \"ho\":\"ah\", \"name\":\"test\"]" {}"" | | {}"" {}"[\"name\":1, 3:\"p\", \"test\":\"foo\", \"ho\":\"ah\", \"name\":\"test\"]" {}"" |
| {}"" {}"[\"name\":1, 3:\"p\", \"name\":\"foo\", \"ho\":\"ah\", \"name\":\"test\"]" {}"" |
--- return --- --- return ---
() ()
--# saved #-- --# saved #--

View file

@ -1,4 +1,4 @@
:f = $(l) :f = $(l)
|{l} |{l}
f{4:3,true:6,"l":8} f{4:3,(true):6,"l":8}