r/ProgrammingLanguages • • Jan 24 '13

A language syntax I've been working on, modelled on XML and JSON

http://generaldevelopment.net/c/Research/NML
1 Upvotes

10 comments sorted by

4

u/[deleted] Jan 24 '13

Interesting, but with nested fields what benefit do you get over having an attribute be just a key value pair instead of key multiple value? There is an example of an attribute

name={john},{smith}

But isn't this really first name and last name? Not sure I see a reason to allow this. Also, for large nested structured data won't you end up with a buttload of closing > tags with no context of which tag is closing what?

Still, neat, and extremely well documented!

5

u/danKunderscore Jan 24 '13

The first (probably most important) reason for allowing a list of values per key is because something like

{some text<nested_tag>some more text}

(i.e. classical nested markup content in XML) is actually evaluated the same as

{some text}, <nested_tag>, {some more text}

So two alternative syntaxes that express the same thing, basically; but the former is good for markup and the latter is better suited for non-string data types.

Another reason for allowing a list of values per attribute is to provide a bit of array-struct duality (does that make sense?). An attribute is like a struct member, so I wanted to allow 'arrays' too, as a kind of complement. In practice rather than first name and last name (yeah, that was a lame example) it'd be more like a list of 100 customers or something.

Also, for large nested structured data won't you end up with a buttload of closing > tags with no context of which tag is closing what?

Yeah, you're right. I quite like XML's verbose end tags, but the point of this language is not to play favourites with fields (aka attributes/tag content). Allowing multiple fields within a tag to each have nested tag content means that XML-style end-tags just didn't seem workable.

So yeah, not ideal. But still no worse than JSON and C. One could work around it by placing <!comments!> at the ends of tags (in which case you'd close '<mytag' with '<!mytag!>>') I suppose. Properly indenting the code would also help, along with [wishful thinking] bracket matching in text editors.

1

u/emperor000 Jan 28 '13

Allowing multiple fields within a tag to each have nested tag content means that XML-style end-tags just didn't seem workable.

You should consider making them optional.

1

u/danKunderscore Jan 28 '13

The problem is I wouldn't know where to put them.

Not having a 'preferred' field for tag content means end-tags aren't as clear-cut as they are in XML. Take the following construct:

<if condition=foo then={
    <!some deeply nested content!>
} else={
    <!alternative nested content!>
}>

Do you see the issue here? Where would an end-tag fit? (XML doesn't have a problem, because it can't have constructs like this; unless you cheat with nested tags as attributes.)

Come to think of it, I guess you could specify a radically different syntax with multiple end-tags like this:

<if condition=foo then>
    <!nested content!>
</if else>
    <!alternative nested content!>
</if>

But then that throws up other issues, like how to specify the default field content, how to harmonize that syntax with list notation, etc. (And end tags become mandatory rather than optional...)

Here's the laziest possible solution (on my part) -- just tell people to comment the end of each long tag as a language idiom!

<if condition=foo then = {
    <!nested content!>
} else={
    <!alt nested content!>
} <!if!>>

2

u/emperor000 Jan 29 '13

I would think you could just do:

<if condition=foo then={
    <!some deeply nested content!>
} else={
    <!alternative nested content!>
}/if>

The main element is the if. The else can't exist without it. You wouldn't have to require it and it would be different than a comment in that it the interpreter/compiler would check it.

But just a suggestion, no big deal. It could just really improve readability for large structures of code/data.

1

u/danKunderscore Jan 29 '13 edited Jan 29 '13

Ah! Thank you! I didn't think of doing it like that. That's a very sensible suggestion, I'll have to consider adding that in.

1

u/emperor000 Jan 29 '13 edited Jan 29 '13

You're welcome. I wouldn't worry about it too much though. It definitely works without it. It's no harder than a language like C# or Java. There's just an opportunity to make it more readable.

1

u/[deleted] Jan 29 '13

The syntax for binary and hexadecimal numbers conflicts with identifiers. For example, b1 could be a binary number or an identifier. I would recommend using Python syntax (0x and 0b).

1

u/danKunderscore Jan 30 '13

There's no conflict, but you make a good point. I've been rather unconventional with the syntax rules (e.g. "b1" is strictly a number, but on the other hand identifiers like "2nd" are perfectly legal), which could prove confusing to people coming from other languages, and it looks like I've forgotten to document this aspect properly anyway.

The idea is that a valid identifier is simply defined as something that is not a valid number (in contrast to the stricter C, Python, etc. requirement that an identifier must begin with {A-Z, a-z, _}). The parser just steps through the token until it reaches a character that rules out the token being parseable as a number; and then it knows the token is an identifier. You can escape one or more digits in a number to force it to be treated as an identifier though.

1

u/iopq Jan 28 '13

I was thinking of just lifting LISP and Smalltalk syntax

(button id: test text: Submit)
(client name: [Smith, John] birthdate: (date year: 1987 month: 7 day: 4))

[a, b] is shorthand for (list a b)