I guess the article could be called Falsehoods Programmers Assume of Programming Language Syntaxes.
Weird Lexical Syntax
41–50 of 234 posts
Re: Weird Lexical Syntax
#42I don't think it's easy to write a good syntax coloring engine like the one in Vim. Syntax coloring has to handle context: different rules for material nested in certain ways. Vim's syntax higlighter lets you declare two kinds of items: matches and regions. Matches are simpler lexical rules, whereas regions have separate expressions for matching the start and end and middle. There are ways to exclude leading and trai…
This is the "genman" script which takes the raw output of a manpage to HTML converter, and massages it to form the HTML version of the TXR manual:
https://www.kylheku.com/cgit/txr/tree/genman.txr
Everything that is white (not colored) is literal template material. Lisp code is embedded in directives, like @(do ...). In this scheme, TXR keywords appear purple, TXR Lisp ones green. They can be the same; see the (and ...) in line 149, versus numerous occurrences of @(and).
Quasistrings contain nested syntax: see 130 where ` ... ` contains an embedded (if ...). That could itself contain a quasistring with more embedded code.
TXR's txr.vim" and tl.vim* syntax definition files are both generated by this:
Re: Weird Lexical Syntax
#43> Every C programmers (sic) knows you can't embed a multi-line comment in a multi-line comment. And every Standard ML programmer might find this to be a surprising limitation. The following is a valid Standard ML program: (* (* Nested (**) *) comment *) val _ = print "hello, world\n" Here is the output: $ sml Given how C was considered one of the "expressive" languages when it arrived, it's curious that nested commen…
Re: Weird Lexical Syntax
#44Another syntax oddity (not mentioned here) that breaks most highlighters: In Java, unicode escapes can be anywhere, not just in strings. For example, the following is a valid class: class Foo\u007b} and this assert will not trigger: assert // String literals can have unicode escapes like \u000A! "Hello World".equals("\u00E4");
I have never seen this in Java! Is there any use cases where it could be useful?
public class Foo {
public static void main(String[] args) {
double \u03c0 = 3.14159265;
System.out.println("\u03c0 = " + \u03c0);
}
}
Output: $ javac Foo.java && java Foo
π = 3.14159265
Of course, nowadays we can simply write this with any decent editor: public class Foo {
public static void main(String[] args) {
double π = 3.14159265;
System.out.println("π = " + π);
}
}
Support for Unicode escape sequences is a result of how the Java Language Specification (JLS) defines InputCharacter. Quoting from Section 3.4 of JLS https://docs.oracle.com/javase/specs/jls/se23/jls23.pdf>: InputCharacter:
UnicodeInputCharacter but not CR or LF
UnicodeInputCharacter is defined as the following in section 3.3: UnicodeInputCharacter:
UnicodeEscape
RawInputCharacter
UnicodeEscape:
\ UnicodeMarker HexDigit HexDigit HexDigit HexDigit
UnicodeMarker:
u {u}
HexDigit:
(one of)
0 1 2 3 4 5 6 7 8 9 a b c d e f A B C D E F
RawInputCharacter:
any Unicode character
As a result the lexical analyser honours Unicode escape sequences absolutely anywhere in the program text. For example, this is a valid Java program: public class Bar {
public static void \u006d\u0061\u0069\u006e(String[] args) {
System.out.println("hello, world");
}
}
Here is the output: $ javac Bar.java && java Bar
hello, world
However, this is an incorrect Java program: public class Baz {
// This comment contains \u6d.
public static void main(String[] args) {
System.out.println("hello, world");
}
}
Here is the error: $ javac Baz.java
Baz.java:2: error: illegal unicode escape
// This comment contains \u6d.
^
1 error
Yes, this is an error even if the illegal Unicode escape sequence occurs in a comment!Re: Weird Lexical Syntax
#45 do_action() ??!??! handle_error()
It almost looks like special error handling syntax but still remains satisfying once you realize it's an || logical-or statement and it's using short circuiting rules to execute handle error if the action returns a non-zero value.Re: Weird Lexical Syntax
#46This was a fun read, but it left me a bit more sympathetic to the lisp perspective, which (if I've understood it) is that syntax, being not an especially important part of a language, is more of a hurdle than a help, and should be as simple and uniform as possible so we can focus on other things. Which is sort of ironic because learning how to do structural editing on lisps has absolutely been more hurdle than help s…
And I think an complex syntax is far easier to read and write than a simple syntax with complex semantics. You also get a faster feedback loop in case the syntax of your code is wrong vs the semantics (which might be undiscovered until runtime).
Re: Weird Lexical Syntax
#47Glad to see confirmed that PHP is the most non weird programming language ;)
$a =
But the spaces have to match, if any line has less spaces than the EOL it gives an error.Re: Weird Lexical Syntax
#48Is this a "no third party dependencies" thing?
Re: Weird Lexical Syntax
#49Re: Weird Lexical Syntax
#50Meanwhile NeoVim doesn’t syntax highlight my commit message properly if I have messed with "commit cleanup" enough. The comment character in Git commit messages can be a problem when you insist on prepending your commits with some "id" and the id starts with `#`. One suggestion was to allow backslash escapes in commit messages since that makes sense to a computer scientist.[1] But looking at all of this lexical stuff…
The comment character is also configurable: git config core.commentchar This is helpful where you want to use use say, markdown to have tidily formatted commit messages make up your pull request body too.