Are you an LLM? Read llms.txt for a summary of the docs, or llms-full.txt for the full context.
Skip to content

Literals

Characters

<bin_digit> ::= "0" | "1" ;
<dec_digit> ::= "0" | "1" | "2" | "3" | "4" | "5" | "6" | "7" | "8" | "9" ;
<hex_digit> ::=
    | "0" | "1" | "2" | "3" | "4" | "5" | "6" | "7" | "8" | "9" | "a"
    | "b" | "c" | "d" | "e" | "f" | "A" | "B" | "C" | "D" | "E" | "F" ;
<alpha_char> ::=
    | "a" | "b" | "c" | "d" | "e" | "f" | "g" | "h" | "i" | "j" | "k" | "l" | "m" | "n" | "o" | "p"
    | "q" | "r" | "s" | "t" | "u" | "v" | "w" | "x" | "y" | "z" ;
<alphanumeric_char> ::= <alpha_char> | <dec_digit> ;

<unicode_char> ::= ? any valid Unicode scalar value ? ;

Numeric literals

<dec_literal> ::= { (<dec_digit> | "_")+ } ;
<hex_literal> ::= { "0x" (<hex_digit> | "_")+ } ;

<numeric_literal> ::= <dec_literal> | <hex_literal> ;

Numeric literals are composed of decimal or hexadecimal digits. Each literal may contain underscores for readability. Hexadecimal literals are prefixed with 0x.

The parser stores integer literals as Lit::Int(u64, Option<PrimitiveType>, Span).

Binary literals

<bin_literal> ::= { "0b" (<bin_digit> | "_")+ } ;

Binary literals are prefixed with 0b and produce Lit::Bin(Vec<u8>, Span).

String literals

<string_literal> ::= ('"' (!'"' <unicode_char>)* '"') | ("'" (!"'" <unicode_char>)* "'") ;

String literals may use either double or single quotes. Both forms support escape sequences: \n, \t, \r, \\, \", \'.

String literals produce Lit::Str(String, Span).

Boolean literals

<boolean_literal> ::= "true" | "false" ;

Literal

<literal> ::= <numeric_literal> | <bin_literal> | <hex_literal> | <string_literal> | <boolean_literal> ;

The <literal> maps to Lit variants in the AST:

SyntaxAST variant
42, 0xFFLit::Int(u64, Option<PrimitiveType>, Span)
"hello"Lit::Str(String, Span)
true, false(see note above)
0xDEADBEEF (bytes)Lit::Hex(Vec<u8>, Span)
0b10101010Lit::Bin(Vec<u8>, Span)

Semantics

Numeric literals may contain arbitrary underscores. The type of a numeric literal is inferred from context; if no type can be inferred, it defaults to u256.

Both numeric and boolean literals are roughly translated to pushing the value onto the EVM stack.

String literals represent string instantiation, which behaves as a packed u8 array instantiation.

const A = 1;
const B = 0xffFFff;
const C = true;
const D = "asdf";
const E = "💩";