metaprogramming


Macro that defines macro returning struct constant


I'm writing tokenizer. I have Token struct:
defmodule Token do
defstruct type: :text, str: ""
end
I want to define some common token constants (Tokens.double_quote) to use in function pattern matching:
defmodule Tokenizer do
require Tokens
#def tokenize(<<char>> <> buf, %Token{type: :double_quote, str: "\""} = current_token, acc) do
def tokenize(<<char>> <> buf, Tokens.double_quote = current_token, acc) do
# ...
end
def tokenize("\"" <> buf, current_token, acc) do
# ...
end
def tokenize(<<char>> <> buf, current_token, acc) do
# ...
end
end
I decided to use macros, because I cannot use functions (not during match) or module attributes (not visible outside module).
Macro returning struct constant works:
defmodule Tokens do
import TokenMacro
# This works
#defmacro double_quote, do: quote do: %Token{type: :double_quote, str: "\""}
# This doesn't work
token double_quote, :double_quote, "\""
end
Macro that defines macro returning struct constant doesnt't work:
defmodule TokenMacro do
defmacro token(name, type, str) do
quote do
defmacro unquote(name), do: %Token{type: unquote(type), str: unquote(str)}
end
end
end
I get CompileError:
tokenizer.ex: invalid quoted expression: %Token{str: "\"", type: :double_quote}
expanding macro: Tokens.double_quote/0
What is wrong with token macro?
Is there simpler solution?

Related Links

Is there a fast way of going from a symbol to a function call in Julia? [duplicate]
Programmatically alter Elixir Code
Elixir - How can I unquote an array of functions in my macro?
Nim reflect on type's field types at compile-time
Save variables to file at runtime
In Julia, is it possible to pass values for evaluation in an Expr object without using global variables?
Metaprogramming in Julia — Splice integer into variable name
What's the difference between `use` and `#before_compile`?
How to define functions based on attribute to elixir?
Remove a const qualifier from a variable in D
Meta-modeling crud FROM/JOIN/WHERE statement fragments for higher-level analysis
Elixir: eval'ing code in module context using Code.eval_quoted/3
What is Declarative Meta-Programming?
Reconsolidate protocols in Elixir 1.2 or higher
Inserting an AST into a quote do end block without unquoting it
Transform quoted elixir code to a code string

Categories

HOME
notepad++
ng-idle
redux-form
pdfbox
opendj
google-apps
bookmarks
crystal-lang
flume
rcloud
magnetic-cards
imessage
pyramid
apache2.4
lilypond
visual-studio-extensions
mongoid6
epsilon
google-api-client
siri
custom-post-type
fable-f#
appirater
ms-access-web-app
ms-access-2003
firebase-analytics
tizen-tv
postback
utorrent
quickfixj
mnist
lubridate
searchkit
swagger-php
docfx
jpad
node-mssql
css-position
escpos
importerror
lampp
spring-integration-sftp
geotiff
launchctl
mongotemplate
aquamacs
scalding
headless
arules
donations
watch-os-2
udpclient
embedding
http-status-code-401
nss
cda
boost-serialization
nullable
halcon
np-spring
page.js
batik
pyobjc
code-documentation
grouping-sets
apache-spark-1.3
jbake
teaspoon
linkedin-jsapi
cbind
ios8-extension
buffering
menubar
conditionaltagsupport
sphinxql
mantle
http-status-code-410
sqlclr
android-loadermanager
web-farm
pyunit
correlated-subquery
extconf.rb
database-create
koken
jsr286
stress
catransform3d
android-holo-everywhere
google-ajax-api
mbeans
clrstoredprocedure
unattended-processing
presentation-layer
objective-j
entitykey
information-management
lemmatization
leader
appliance

Resources

Mobile Apps Dev
Database Users
javascript
java
csharp
php
android
MS Developer
developer works
python
ios
c
html
jquery
RDBMS discuss
Cloud Virtualization
Database Dev&Adm
javascript
java
csharp
php
python
android
jquery
ruby
ios
html
Mobile App
Mobile App
Mobile App