Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How does a non-standard string literal avoid a syntax error generated by a standard string literal?

Tags:

julia

Based on the relevant section of the Julia docs, my understanding is that a non-standard string literal like foo"hello, world" is equivalent to explicitly calling the corresponding macro: @foo_str("hello, world"). However, there must be some extra magic that I'm not understanding. Consider a date format dateformat"\m". By itself, "\m" throws a syntax error:

julia> "\m"
ERROR: syntax: invalid escape sequence

And the same syntax error is thrown if I call @dateformat_str("\m"), since the string literal "\m" appears to be evaluated or error checked before it is passed to the macro:

julia> using Dates

julia> @dateformat_str("\m")
ERROR: syntax: invalid escape sequence

However, using the non-standard string literal works:

julia> dateformat"\m"
dateformat"\m"

This is counter-intuitive, because I thought that dateformat"\m" was equivalent to @dateformat_str("\m"). How does the non-standard string literal avoid the syntax error generated by the standard string literal?

like image 471
Cameron Bieganek Avatar asked Sep 25 '19 21:09

Cameron Bieganek


People also ask

How do you escape a string literal?

String literal syntaxUse the escape sequence \\ to represent a backslash character as part of the string. You can represent a single quotation mark symbol either by itself or with the escape sequence \' . You must use the escape sequence \" to represent a double quotation mark.

What is the syntax of string literal?

A "string literal" is a sequence of characters from the source character set enclosed in double quotation marks (" "). String literals are used to represent a sequence of characters which, taken together, form a null-terminated string. You must always prefix wide-string literals with the letter L.

What is unterminated string literal error in Python?

you have opening and closing quotes (single or double) for your string literal, you have escaped your string literal correctly, your string literal isn't split across multiple lines.


1 Answers

In short, because the parser recognizes that situation and parses the string literal differently

For string macros invocations it does this. Calling: parse-raw-literal

Where as for normal string literals it does this. Calling parse-string-literal


@dateformat_str("\m") on the other hand parses as a macro invocation on a normal string literal. so it uses the later parse-string-literal, which errors.

Note that ones parsed it has parsed the string into what is escaped as "\\m"

julia> dump(:(dateformat"\m"))
Expr
  head: Symbol macrocall
  args: Array{Any}((3,))
    1: Symbol @dateformat_str
    2: LineNumberNode
      line: Int64 1
      file: Symbol REPL[6]
    3: String "\\m

Of related interest is the raw string macro , which does nothing at all, but is still going to be string parsed using parse-raw-literal It is basically defined as

macro raw_str(s)
    return s
end
like image 51
Lyndon White Avatar answered Sep 22 '22 07:09

Lyndon White