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

How can I write metrics into my software that speak to investors while standing up to geek scrutiny? [closed]
CoffeeScript dynamic methods
Tool for automated porting and language that can compile into others
HAML meta-programming
Computability & complexity application [closed]
REBOL metaprogramming questions
How do I replace a message body programmatically in Smalltalk?
Are there real world applications that use metaprogramming?
How to download MQL4 IDE and Editor?
Can I add numbers with the C/C++ preprocessor?
Meta Programming, whats it good for?
Using metamorphic code to reduce boilerplate
Typed metaprogramming languages [closed]
What to read as a good intro and quickstart to aspect-oriented programming and metaprogramming?
Examples of what D’s templates can be used for
Using Polymorphic Code for Legitimate Purposes?

Categories

HOME
paraview
codenvy
spring-data-redis
shiro
simulink
alsa
onclick
vsm
python-multithreading
frontend
drop-down-menu
google-chrome-app
appcelerator-studio
libc
zerobrane
qsqlquery
package.json
symfony-2.8
google-content-api
google-data-studio
ag
springfox
imageresizer
epsilon
expressionengine3
definitelytyped
maze
syntax-highlighting
stackexchange
calculation
hibernate-search
browser-extension
openproject
laravel-elixir
apriori
oracle-service-bus
http-request
android-checkbox
facebook-social-plugins
httr
ntvs
proget
sonarlint-eclipse
hammer.js
manova
page-replacement
payara-micro
context-switch
wpas
excel-dna
jquery-jscrollpane
boost-serialization
grass
asyncdisplaykit
libvlc
toran-proxy
r-package
atoi
ocra
cwrsync
ansi
jquery-forms-plugin
j-security-check
dot.js
cryptographichashfunction
contrast
iso-prolog
human-computer-interface
universal-image-loader
named-parameters
personalization
scriptaculous
http-patch
ice-cube
unit-of-work
neos-server
joomla-template
android-icons
robotlegs
nokia-s40
pyunit
josql
database-create
jquery-tools
exponentiation
tmx
object-database
aspmenu
zend-rest
zen
tabpanel
wordprocessingml
entitykey
product-management
httpcookie

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