Add lots of static Symbols.

These will be used in the subsequent commits. Many of them are
attributes.

The commit also adds the ability to handle symbols that aren't
identifiers (e.g. "proc-macro").
This commit is contained in:
Nicholas Nethercote 2019-05-07 09:55:12 +10:00
parent 1764b29725
commit a8245f5a38
2 changed files with 415 additions and 16 deletions

View file

@ -11,7 +11,7 @@ use quote::quote;
#[allow(non_camel_case_types)] #[allow(non_camel_case_types)]
mod kw { mod kw {
syn::custom_keyword!(Keywords); syn::custom_keyword!(Keywords);
syn::custom_keyword!(Other); syn::custom_keyword!(Symbols);
} }
struct Keyword { struct Keyword {
@ -33,14 +33,24 @@ impl Parse for Keyword {
} }
} }
struct Symbol(Ident); struct Symbol {
name: Ident,
value: Option<LitStr>,
}
impl Parse for Symbol { impl Parse for Symbol {
fn parse(input: ParseStream<'_>) -> Result<Self> { fn parse(input: ParseStream<'_>) -> Result<Self> {
let ident: Ident = input.parse()?; let name = input.parse()?;
let value = match input.parse::<Token![:]>() {
Ok(_) => Some(input.parse()?),
Err(_) => None,
};
input.parse::<Token![,]>()?; input.parse::<Token![,]>()?;
Ok(Symbol(ident)) Ok(Symbol {
name,
value,
})
} }
} }
@ -69,7 +79,7 @@ impl Parse for Input {
braced!(content in input); braced!(content in input);
let keywords = content.parse()?; let keywords = content.parse()?;
input.parse::<kw::Other>()?; input.parse::<kw::Symbols>()?;
let content; let content;
braced!(content in input); braced!(content in input);
let symbols = content.parse()?; let symbols = content.parse()?;
@ -116,19 +126,22 @@ pub fn symbols(input: TokenStream) -> TokenStream {
} }
for symbol in &input.symbols.0 { for symbol in &input.symbols.0 {
let value = &symbol.0; let name = &symbol.name;
let value_str = value.to_string(); let value = match &symbol.value {
check_dup(&value_str); Some(value) => value.value(),
None => name.to_string(),
};
check_dup(&value);
prefill_stream.extend(quote! { prefill_stream.extend(quote! {
#value_str, #value,
}); });
symbols_stream.extend(quote! { symbols_stream.extend(quote! {
pub const #value: Symbol = Symbol::new(#counter); pub const #name: Symbol = Symbol::new(#counter);
}); });
counter += 1; counter += 1;
} }
TokenStream::from(quote! { let tt = TokenStream::from(quote! {
macro_rules! keywords { macro_rules! keywords {
() => { () => {
#keyword_stream #keyword_stream
@ -159,5 +172,11 @@ pub fn symbols(input: TokenStream) -> TokenStream {
]) ])
} }
} }
}) });
// To see the generated code generated, uncomment this line, recompile, and
// run the resulting output through `rustfmt`.
//eprintln!("{}", tt);
tt
} }

View file

@ -99,88 +99,375 @@ symbols! {
Union: "union", Union: "union",
} }
// Other symbols that can be referred to with syntax_pos::symbols::* // Symbols that can be referred to with syntax_pos::symbols::*. The symbol
Other { // is the stringified identifier unless otherwise specified (e.g.
// `proc_dash_macro` represents "proc-macro").
Symbols {
aarch64_target_feature,
abi,
abi_amdgpu_kernel,
abi_msp430_interrupt,
abi_ptx,
abi_sysv64,
abi_thiscall,
abi_unadjusted,
abi_vectorcall,
abi_x86_interrupt,
aborts,
advanced_slice_patterns,
adx_target_feature,
alias, alias,
align, align,
alignstack,
all,
allocator,
allocator_internals,
alloc_error_handler, alloc_error_handler,
allow, allow,
allowed,
allow_fail, allow_fail,
allow_internal_unsafe, allow_internal_unsafe,
allow_internal_unstable, allow_internal_unstable,
allow_internal_unstable_backcompat_hack,
always,
any,
arbitrary_self_types,
arm_target_feature,
asm,
associated_consts,
associated_type_defaults,
associated_types,
async_await,
attr,
attributes,
attr_literals,
augmented_assignments,
automatically_derived, automatically_derived,
avx512_target_feature,
await_macro,
bin,
bind_by_move_pattern_guards,
block,
borrowck_graphviz_postflow,
borrowck_graphviz_preflow,
box_patterns,
box_syntax,
braced_empty_structs,
C,
cdylib,
cfg, cfg,
cfg_attr, cfg_attr,
cfg_attr_multi,
cfg_target_feature,
cfg_target_has_atomic,
cfg_target_thread_local,
cfg_target_vendor,
clone,
clone_closures,
clone_from,
closure_to_fn_coercion,
cmpxchg16b_target_feature,
cold, cold,
compile_error,
compiler_builtins, compiler_builtins,
concat_idents,
conservative_impl_trait,
console,
const_compare_raw_pointers,
const_fn,
const_fn_union,
const_generics,
const_indexing,
const_let,
const_panic,
const_raw_ptr_deref,
const_raw_ptr_to_usize_cast,
const_transmute,
contents,
convert,
copy_closures,
core,
core_intrinsics,
crate_id, crate_id,
crate_in_paths,
crate_name, crate_name,
crate_type, crate_type,
crate_visibility_modifier,
custom_attribute,
custom_derive,
custom_inner_attributes,
custom_test_frameworks,
c_variadic,
decl_macro,
default_lib_allocator, default_lib_allocator,
default_type_parameter_fallback,
default_type_params,
deny, deny,
deprecated, deprecated,
derive, derive,
doc, doc,
doc_alias,
doc_cfg,
doc_keyword,
doc_masked,
doc_spotlight,
document_private_items,
dotdoteq_in_patterns,
dotdot_in_tuple_patterns,
dropck_eyepatch,
dropck_parametricity,
drop_types_in_const,
dylib,
dyn_trait,
eh_personality,
eh_unwind_resume,
enable,
Err,
except,
exclusive_range_pattern,
exhaustive_integer_patterns,
exhaustive_patterns,
existential_type,
expected,
export_name, export_name,
extern_absolute_paths,
external_doc,
extern_crate_item_prelude,
extern_crate_self,
extern_in_paths,
extern_prelude,
extern_types,
f16c_target_feature,
feature, feature,
ffi_returns_twice, ffi_returns_twice,
field_init_shorthand,
file,
fn_must_use,
forbid, forbid,
format_args_nl,
from,
From,
from_error,
from_generator,
from_ok,
fundamental, fundamental,
future,
Future,
generators,
generic_associated_types,
generic_param_attrs,
global_allocator, global_allocator,
global_asm,
globs,
hexagon_target_feature,
hidden,
homogeneous_aggregate,
html_favicon_url,
html_logo_url,
html_no_source,
html_playground_url,
html_root_url,
i128,
i128_type,
i16,
i32,
i64,
i8,
ident,
if_let,
if_while_or_patterns,
ignore, ignore,
impl_header_lifetime_elision,
impl_trait_in_bindings,
import_shadowing,
in_band_lifetimes,
include, include,
inclusive_range_syntax,
infer_outlives_requirements,
infer_static_outlives_requirements,
inline, inline,
intel,
into_iter,
IntoIterator,
into_result,
intrinsics,
irrefutable_let_patterns,
isize,
issue,
issue_5723_bootstrap,
issue_tracker_base_url,
item_like_imports,
iter,
Iterator,
keyword, keyword,
kind,
label,
label_break_value,
lang, lang,
lang_items,
lib,
link, link,
linkage,
link_args, link_args,
link_cfg,
link_llvm_intrinsics,
link_name, link_name,
link_section, link_section,
linkage, lint_reasons,
local_inner_macros,
log_syntax,
loop_break_value,
macro_at_most_once_rep,
macro_escape, macro_escape,
macro_export, macro_export,
macro_lifetime_matcher,
macro_literal_matcher,
macro_reexport,
macro_rules,
macros_in_extern,
macro_use, macro_use,
macro_vis_matcher,
main, main,
managed_boxes,
marker, marker,
marker_trait_attr,
masked, masked,
match_beginning_vert,
match_default_bindings,
may_dangle, may_dangle,
message,
min_const_fn,
min_const_unsafe_fn,
mips_target_feature,
mmx_target_feature,
module,
more_struct_aliases,
movbe_target_feature,
must_use, must_use,
naked, naked,
naked_functions,
name,
needs_allocator, needs_allocator,
needs_panic_runtime, needs_panic_runtime,
negate_unsigned,
never,
never_type,
next,
nll,
no_builtins, no_builtins,
no_core, no_core,
no_crate_inject,
no_debug, no_debug,
no_default_passes,
no_implicit_prelude, no_implicit_prelude,
no_inline,
no_link, no_link,
no_main, no_main,
no_mangle, no_mangle,
non_ascii_idents,
None,
non_exhaustive,
non_modrs_mods,
no_stack_check,
no_start, no_start,
no_std, no_std,
non_exhaustive, not,
note,
Ok,
omit_gdb_pretty_printer_section, omit_gdb_pretty_printer_section,
on,
on_unimplemented,
oom,
ops,
optimize, optimize,
optimize_attribute,
optin_builtin_traits,
option,
Option,
opt_out_copy,
overlapping_marker_traits,
packed,
panic_handler, panic_handler,
panic_impl,
panic_implementation,
panic_runtime, panic_runtime,
passes,
path, path,
pattern_parentheses,
Pending,
pin,
Pin,
platform_intrinsics,
plugin, plugin,
plugin_registrar, plugin_registrar,
plugins,
Poll,
poll_with_tls_context,
powerpc_target_feature,
precise_pointer_size_matching,
prelude,
prelude_import, prelude_import,
primitive,
proc_dash_macro: "proc-macro",
proc_macro, proc_macro,
proc_macro_attribute, proc_macro_attribute,
proc_macro_derive, proc_macro_derive,
proc_macro_expr,
proc_macro_gen,
proc_macro_hygiene,
proc_macro_mod,
proc_macro_non_items,
proc_macro_path_invoc,
profiler_runtime, profiler_runtime,
pub_restricted,
pushpop_unsafe,
quad_precision_float,
question_mark,
quote,
Range,
RangeFrom,
RangeFull,
RangeInclusive,
RangeTo,
RangeToInclusive,
raw_identifiers,
Ready,
reason,
recursion_limit, recursion_limit,
reexport_test_harness_main, reexport_test_harness_main,
reflect,
relaxed_adts,
repr, repr,
repr128,
repr_align,
repr_align_enum,
repr_packed,
repr_simd,
repr_transparent,
re_rebalance_coherence,
result,
Result,
Return,
rlib,
rtm_target_feature,
rust,
rust_2015_preview,
rust_2018_preview,
rust_begin_unwind,
rustc_allocator_nounwind,
rustc_allow_const_fn_ptr,
rustc_args_required_const, rustc_args_required_const,
rustc_attrs,
rustc_clean, rustc_clean,
rustc_const_unstable, rustc_const_unstable,
rustc_conversion_suggestion, rustc_conversion_suggestion,
rustc_copy_clone_marker, rustc_copy_clone_marker,
rustc_def_path, rustc_def_path,
rustc_deprecated, rustc_deprecated,
rustc_diagnostic_macros,
rustc_dirty, rustc_dirty,
rustc_doc_only_macro,
rustc_dump_env_program_clauses,
rustc_dump_program_clauses, rustc_dump_program_clauses,
rustc_dump_user_substs, rustc_dump_user_substs,
rustc_error, rustc_error,
@ -191,13 +478,21 @@ symbols! {
rustc_layout_scalar_valid_range_end, rustc_layout_scalar_valid_range_end,
rustc_layout_scalar_valid_range_start, rustc_layout_scalar_valid_range_start,
rustc_mir, rustc_mir,
rustc_object_lifetime_default,
rustc_on_unimplemented, rustc_on_unimplemented,
rustc_outlives, rustc_outlives,
rustc_paren_sugar, rustc_paren_sugar,
rustc_partition_codegened, rustc_partition_codegened,
rustc_partition_reused, rustc_partition_reused,
rustc_peek,
rustc_peek_definite_init,
rustc_peek_maybe_init,
rustc_peek_maybe_uninit,
rustc_private,
rustc_proc_macro_decls, rustc_proc_macro_decls,
rustc_promotable,
rustc_regions, rustc_regions,
rustc_stable,
rustc_std_internal_symbol, rustc_std_internal_symbol,
rustc_symbol_name, rustc_symbol_name,
rustc_synthetic, rustc_synthetic,
@ -205,23 +500,108 @@ symbols! {
rustc_then_this_would_need, rustc_then_this_would_need,
rustc_transparent_macro, rustc_transparent_macro,
rustc_variance, rustc_variance,
rustdoc,
rust_eh_personality,
rust_eh_unwind_resume,
rust_oom,
__rust_unstable_column,
rvalue_static_promotion,
sanitizer_runtime, sanitizer_runtime,
self_in_typedefs,
self_struct_ctor,
Send,
should_panic, should_panic,
simd, simd,
simd_ffi,
since,
size,
slice_patterns,
slicing_syntax,
Some,
specialization,
speed,
spotlight, spotlight,
sse4a_target_feature,
stable, stable,
staged_api,
start, start,
static_in_const,
staticlib,
static_nobundle,
static_recursion,
std,
stmt_expr_attributes,
stop_after_dataflow,
struct_field_attributes,
struct_inherit,
structural_match, structural_match,
struct_variant,
suggestion,
target_feature, target_feature,
target_has_atomic,
target_thread_local,
task,
tbm_target_feature,
termination_trait,
termination_trait_test,
test,
test_2018_feature,
test_accepted_feature,
test_removed_feature,
test_runner, test_runner,
thread_local, thread_local,
tool_attributes,
tool_lints,
trace_macros,
trait_alias,
transmute,
transparent,
trivial_bounds,
Try,
try_blocks,
tuple_indexing,
ty,
type_alias_enum_variants,
type_ascription,
type_length_limit, type_length_limit,
type_macros,
u128,
u16,
u32,
u64,
u8,
unboxed_closures,
underscore_const_names,
underscore_imports,
underscore_lifetimes,
uniform_paths,
universal_impl_trait,
unmarked_api,
unrestricted_attribute_tokens,
unsafe_destructor_blind_to_params, unsafe_destructor_blind_to_params,
unsafe_no_drop_flag,
unsized_locals,
unsized_tuple_coercion,
unstable, unstable,
untagged_unions,
unwind, unwind,
unwind_attributes,
used, used,
use_extern_macros,
use_nested_groups,
usize,
v1,
vis,
visible_private_types,
volatile,
warn, warn,
warn_directory_ownership,
wasm_import_module,
wasm_target_feature,
while_let,
windows,
windows_subsystem, windows_subsystem,
Yield,
} }
} }