-
Notifications
You must be signed in to change notification settings - Fork 39
(WIP) Initial work on nimble_parsec generated lexer #448
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| @@ -0,0 +1,189 @@ | ||||||||||||||||||||||||||||||||
| defmodule Thrift.Parser.Nimble do | ||||||||||||||||||||||||||||||||
| import NimbleParsec | ||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||
| defmodule Lexer do | ||||||||||||||||||||||||||||||||
| @moduledoc false | ||||||||||||||||||||||||||||||||
| @punctuator ~c"(){}[]<>,;=*" | ||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||
| @keywords ~w( | ||||||||||||||||||||||||||||||||
| namespace include cpp_include | ||||||||||||||||||||||||||||||||
| typedef enum union struct exception | ||||||||||||||||||||||||||||||||
| void bool byte i8 i16 i32 i64 double string binary list map set | ||||||||||||||||||||||||||||||||
| const oneway extends throws service required optional | ||||||||||||||||||||||||||||||||
| true false | ||||||||||||||||||||||||||||||||
| ) | ||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||
| def token(combinator \\ empty()) do | ||||||||||||||||||||||||||||||||
| choice(combinator, [ | ||||||||||||||||||||||||||||||||
| keyword(), | ||||||||||||||||||||||||||||||||
| identifier(), | ||||||||||||||||||||||||||||||||
| literal(), | ||||||||||||||||||||||||||||||||
| number(), | ||||||||||||||||||||||||||||||||
| punctuator(), | ||||||||||||||||||||||||||||||||
| whitespace() | ||||||||||||||||||||||||||||||||
| ]) | ||||||||||||||||||||||||||||||||
| end | ||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||
| defp whitespace() do | ||||||||||||||||||||||||||||||||
| ascii_string([?\s, ?\t, ?\n, ?\v, ?\f, ?\r], min: 1) | ||||||||||||||||||||||||||||||||
| |> ignore() | ||||||||||||||||||||||||||||||||
| |> label("whitespace") | ||||||||||||||||||||||||||||||||
| end | ||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||
| defp keyword() do | ||||||||||||||||||||||||||||||||
| @keywords | ||||||||||||||||||||||||||||||||
| |> Enum.map(&(string(&1) |> replace(String.to_atom(&1)))) | ||||||||||||||||||||||||||||||||
| |> choice() | ||||||||||||||||||||||||||||||||
| |> label("keyword") | ||||||||||||||||||||||||||||||||
| end | ||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||
| defp punctuator() do | ||||||||||||||||||||||||||||||||
| @punctuator | ||||||||||||||||||||||||||||||||
| |> Enum.map(&(string(<<&1>>) |> replace(String.to_atom(<<&1>>)))) | ||||||||||||||||||||||||||||||||
| |> choice() | ||||||||||||||||||||||||||||||||
| |> label("punctuator") | ||||||||||||||||||||||||||||||||
| end | ||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||
| defp literal() do | ||||||||||||||||||||||||||||||||
| choice([ | ||||||||||||||||||||||||||||||||
| literal_with(?"), | ||||||||||||||||||||||||||||||||
| literal_with(?') | ||||||||||||||||||||||||||||||||
| ]) | ||||||||||||||||||||||||||||||||
| |> reduce({List, :to_string, []}) | ||||||||||||||||||||||||||||||||
| |> label("literal") | ||||||||||||||||||||||||||||||||
| end | ||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||
| defp literal_with(char) do | ||||||||||||||||||||||||||||||||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Perhaps |
||||||||||||||||||||||||||||||||
| delim = ascii_char([char]) | ||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||
| delim | ||||||||||||||||||||||||||||||||
| |> ignore() | ||||||||||||||||||||||||||||||||
| |> concat( | ||||||||||||||||||||||||||||||||
| choice([ | ||||||||||||||||||||||||||||||||
| utf8_char([?\\]) |> ignore() |> concat(delim), | ||||||||||||||||||||||||||||||||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Does this handle embedded newlines, etc. like we support in the current lexer? elixir-thrift/src/thrift_lexer.xrl Lines 91 to 105 in 1610f61
|
||||||||||||||||||||||||||||||||
| utf8_char([]), | ||||||||||||||||||||||||||||||||
| error(eos(), "expected literal delimiter ?#{[char]}"), | ||||||||||||||||||||||||||||||||
| error(empty(), "expected utf8 codepoint") | ||||||||||||||||||||||||||||||||
| ]) | ||||||||||||||||||||||||||||||||
| |> repeat_until([delim]) | ||||||||||||||||||||||||||||||||
| ) | ||||||||||||||||||||||||||||||||
| |> ignore(delim) | ||||||||||||||||||||||||||||||||
| end | ||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||
| defp number(combinator \\ empty()) do | ||||||||||||||||||||||||||||||||
| combinator | ||||||||||||||||||||||||||||||||
| |> choice([ | ||||||||||||||||||||||||||||||||
| ascii_char([?-, ?+]) | ||||||||||||||||||||||||||||||||
| |> choice([ | ||||||||||||||||||||||||||||||||
| unsigned_number(), | ||||||||||||||||||||||||||||||||
| empty() | ||||||||||||||||||||||||||||||||
| |> error("expected number") | ||||||||||||||||||||||||||||||||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think this reads a little better as (like you do above): error(empty(), "expected number") |
||||||||||||||||||||||||||||||||
| ]) | ||||||||||||||||||||||||||||||||
| |> post_traverse({__MODULE__, :__sign__, []}), | ||||||||||||||||||||||||||||||||
| unsigned_number() | ||||||||||||||||||||||||||||||||
| ]) | ||||||||||||||||||||||||||||||||
| |> label("number") | ||||||||||||||||||||||||||||||||
| end | ||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||
| defp unsigned_number() do | ||||||||||||||||||||||||||||||||
| choice([ | ||||||||||||||||||||||||||||||||
| hex(), | ||||||||||||||||||||||||||||||||
| integer(min: 1) | ||||||||||||||||||||||||||||||||
| |> choice([ | ||||||||||||||||||||||||||||||||
| ignore(ascii_char([?.])) | ||||||||||||||||||||||||||||||||
| |> integer(min: 1) | ||||||||||||||||||||||||||||||||
| |> optional(ignore(ascii_char([?E, ?e])) |> exponent()), | ||||||||||||||||||||||||||||||||
| empty() | ||||||||||||||||||||||||||||||||
| |> replace(0) | ||||||||||||||||||||||||||||||||
| |> ignore(ascii_char([?E, ?e])) | ||||||||||||||||||||||||||||||||
| |> exponent(), | ||||||||||||||||||||||||||||||||
| empty() | ||||||||||||||||||||||||||||||||
| ]) | ||||||||||||||||||||||||||||||||
| ]) | ||||||||||||||||||||||||||||||||
| |> optional( | ||||||||||||||||||||||||||||||||
| ascii_char([?.]) | ||||||||||||||||||||||||||||||||
| |> ignore() | ||||||||||||||||||||||||||||||||
| |> error(empty(), "expected integer fraction for significand") | ||||||||||||||||||||||||||||||||
| ) | ||||||||||||||||||||||||||||||||
| |> post_traverse({__MODULE__, :__number__, []}) | ||||||||||||||||||||||||||||||||
| end | ||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||
| def __sign__(_rest, acc, context, _line, _offset) do | ||||||||||||||||||||||||||||||||
| case acc do | ||||||||||||||||||||||||||||||||
| [number, ?-] -> | ||||||||||||||||||||||||||||||||
| {[-number], context} | ||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||
| [number, ?+] -> | ||||||||||||||||||||||||||||||||
| {[number], context} | ||||||||||||||||||||||||||||||||
| end | ||||||||||||||||||||||||||||||||
| end | ||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||
| defp hex() do | ||||||||||||||||||||||||||||||||
| string("0x") | ||||||||||||||||||||||||||||||||
| |> ignore() | ||||||||||||||||||||||||||||||||
| |> choice([ | ||||||||||||||||||||||||||||||||
| ascii_string([?0..?9, ?a..?f, ?A..?F], min: 1) | ||||||||||||||||||||||||||||||||
| |> map({String, :to_integer, [16]}), | ||||||||||||||||||||||||||||||||
| empty() | ||||||||||||||||||||||||||||||||
| |> error("expected hexidecimal digit") | ||||||||||||||||||||||||||||||||
| ]) | ||||||||||||||||||||||||||||||||
| end | ||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||
| defp exponent(combinator) do | ||||||||||||||||||||||||||||||||
| combinator | ||||||||||||||||||||||||||||||||
| |> choice([ | ||||||||||||||||||||||||||||||||
| choice([ | ||||||||||||||||||||||||||||||||
| ascii_char([?-, ?+]), | ||||||||||||||||||||||||||||||||
| empty() |> replace(?+) | ||||||||||||||||||||||||||||||||
| ]) | ||||||||||||||||||||||||||||||||
| |> integer(min: 1), | ||||||||||||||||||||||||||||||||
| empty() | ||||||||||||||||||||||||||||||||
| |> error("expected integer exponent") | ||||||||||||||||||||||||||||||||
| ]) | ||||||||||||||||||||||||||||||||
| end | ||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||
| def __number__(_rest, acc, context, _line, _offset) do | ||||||||||||||||||||||||||||||||
| case acc do | ||||||||||||||||||||||||||||||||
| [_int] -> | ||||||||||||||||||||||||||||||||
| {acc, context} | ||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||
| [fraction, int] -> | ||||||||||||||||||||||||||||||||
| {[String.to_float("#{int}.#{fraction}")], context} | ||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||
| [exponent, exponent_sign, fraction, int] -> | ||||||||||||||||||||||||||||||||
| {[String.to_float("#{int}.#{fraction}e#{[exponent_sign]}#{exponent}")], context} | ||||||||||||||||||||||||||||||||
| end | ||||||||||||||||||||||||||||||||
| end | ||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||
| def identifier() do | ||||||||||||||||||||||||||||||||
| ascii_char([?a..?z, ?A..?Z, ?_]) | ||||||||||||||||||||||||||||||||
| |> repeat(ascii_char([?a..?z, ?A..?Z, ?_, ?0..?9])) | ||||||||||||||||||||||||||||||||
| |> reduce({List, :to_atom, []}) | ||||||||||||||||||||||||||||||||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Consider extracting these three lines into their own function because we repeat them below. |
||||||||||||||||||||||||||||||||
| |> optional( | ||||||||||||||||||||||||||||||||
| repeat( | ||||||||||||||||||||||||||||||||
| ascii_char([?.]) | ||||||||||||||||||||||||||||||||
| |> ignore() | ||||||||||||||||||||||||||||||||
| |> choice([ | ||||||||||||||||||||||||||||||||
| ascii_char([?a..?z, ?A..?Z, ?_]) | ||||||||||||||||||||||||||||||||
| |> repeat(ascii_char([?a..?z, ?A..?Z, ?_, ?0..?9])) | ||||||||||||||||||||||||||||||||
| |> reduce({List, :to_atom, []}), | ||||||||||||||||||||||||||||||||
| empty() | ||||||||||||||||||||||||||||||||
| |> error("expected alphabetic character or underscore to continue identifier") | ||||||||||||||||||||||||||||||||
| ]) | ||||||||||||||||||||||||||||||||
| ) | ||||||||||||||||||||||||||||||||
| ) | ||||||||||||||||||||||||||||||||
| |> wrap() | ||||||||||||||||||||||||||||||||
| |> label("identifier") | ||||||||||||||||||||||||||||||||
| end | ||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||
| defp error(combinator \\ empty(), to_error, label) do | ||||||||||||||||||||||||||||||||
| pre_traverse(combinator, to_error, {__MODULE__, :__error__, [label]}) | ||||||||||||||||||||||||||||||||
| end | ||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||
| def __error__(_rest, _acc, _context, _line, _offset, label) do | ||||||||||||||||||||||||||||||||
| {:error, label} | ||||||||||||||||||||||||||||||||
| end | ||||||||||||||||||||||||||||||||
| end | ||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||
| defparsec(:parse_token, Lexer.token()) | ||||||||||||||||||||||||||||||||
| end | ||||||||||||||||||||||||||||||||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -83,6 +83,10 @@ defmodule Thrift.Mixfile do | |
| {:credo, "~> 1.0", only: :dev, runtime: false}, | ||
| {:dialyxir, "~> 0.5", only: :dev, runtime: false}, | ||
|
|
||
| # Compile | ||
| {:nimble_parsec, "~> 0.4", | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 0.5 was just released. |
||
| github: "plataformatec/nimble_parsec", runtime: false, override: true}, | ||
|
|
||
| # Runtime | ||
| {:connection, "~> 1.0"}, | ||
| {:ranch, "~> 1.6"} | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,93 @@ | ||
| defmodule Thrift.Parser.NimbleTest do | ||
| use ExUnit.Case, async: true | ||
| import Thrift.Parser.Nimble, only: [parse_token: 1] | ||
|
|
||
| describe "parse_token/1" do | ||
| test "returns ok on integer" do | ||
| assert parse_token("111") == {:ok, [111], "", %{}, {1, 0}, 3} | ||
| assert parse_token("-111") == {:ok, [-111], "", %{}, {1, 0}, 4} | ||
| assert parse_token("+111") == {:ok, [111], "", %{}, {1, 0}, 4} | ||
| end | ||
|
|
||
| test "returns error on invalid partial number" do | ||
| assert parse_token("-A") == {:error, "expected number", "A", %{}, {1, 0}, 1} | ||
| assert parse_token("+") == {:error, "expected number", "", %{}, {1, 0}, 1} | ||
| end | ||
|
|
||
| test "returns ok on hex" do | ||
| assert parse_token("0x1F") == {:ok, [31], "", %{}, {1, 0}, 4} | ||
| assert parse_token("-0x1a2") == {:ok, [-418], "", %{}, {1, 0}, 6} | ||
| assert parse_token("+0x0FF0") == {:ok, [4080], "", %{}, {1, 0}, 7} | ||
| end | ||
|
|
||
| test "returns error on invalid partial hex" do | ||
| assert parse_token("0xG") == {:error, "expected hexidecimal digit", "G", %{}, {1, 0}, 2} | ||
| assert parse_token("0x") == {:error, "expected hexidecimal digit", "", %{}, {1, 0}, 2} | ||
| end | ||
|
|
||
| test "returns ok on double" do | ||
| assert parse_token("0.0") == {:ok, [0.0], "", %{}, {1, 0}, 3} | ||
| assert parse_token("-1.0") == {:ok, [-1.0], "", %{}, {1, 0}, 4} | ||
| assert parse_token("+1.0") == {:ok, [1.0], "", %{}, {1, 0}, 4} | ||
| assert parse_token("1e0") == {:ok, [1.0], "", %{}, {1, 0}, 3} | ||
| assert parse_token("-2E1") == {:ok, [-20.0], "", %{}, {1, 0}, 4} | ||
| assert parse_token("+3.2e1") == {:ok, [32.0], "", %{}, {1, 0}, 6} | ||
| assert parse_token("43.2E-1") == {:ok, [4.32], "", %{}, {1, 0}, 7} | ||
| assert parse_token("-5.432E+1") == {:ok, [-54.32], "", %{}, {1, 0}, 9} | ||
| end | ||
|
|
||
| test "returns error on invalid partial double" do | ||
| assert parse_token("0.a") == | ||
| {:error, "expected integer fraction for significand", "a", %{}, {1, 0}, 2} | ||
|
|
||
| assert parse_token("1.") == | ||
| {:error, "expected integer fraction for significand", "", %{}, {1, 0}, 2} | ||
|
|
||
| assert parse_token("0e!") == {:error, "expected integer exponent", "!", %{}, {1, 0}, 2} | ||
| assert parse_token("0E+e") == {:error, "expected integer exponent", "+e", %{}, {1, 0}, 2} | ||
| assert parse_token("0E") == {:error, "expected integer exponent", "", %{}, {1, 0}, 2} | ||
| end | ||
|
|
||
| test "returns ok on literal" do | ||
| assert parse_token(~s("hi")) == {:ok, ["hi"], "", %{}, {1, 0}, 4} | ||
| assert parse_token(~s('hello')) == {:ok, ["hello"], "", %{}, {1, 0}, 7} | ||
| assert parse_token(~s("hi 'world'")) == {:ok, ["hi 'world'"], "", %{}, {1, 0}, 12} | ||
| assert parse_token(~s("hi \\"world\\"")) == {:ok, ["hi \"world\""], "", %{}, {1, 0}, 14} | ||
| assert parse_token(~s('hello \\'world\\'')) == {:ok, ["hello 'world'"], "", %{}, {1, 0}, 17} | ||
| end | ||
|
|
||
| test "returns error on invalid partial literal" do | ||
| assert parse_token(~s("hi)) == | ||
| {:error, "expected literal delimiter ?\"", "", %{}, {1, 0}, 3} | ||
|
|
||
| assert parse_token(~s("hello) <> <<128>>) == | ||
| {:error, "expected utf8 codepoint", <<128>>, %{}, {1, 0}, 6} | ||
| end | ||
|
|
||
| test "returns ok on identifier" do | ||
| assert parse_token("hi") == {:ok, [[:hi]], "", %{}, {1, 0}, 2} | ||
| assert parse_token("Hello") == {:ok, [[:Hello]], "", %{}, {1, 0}, 5} | ||
| assert parse_token("_hey") == {:ok, [[:_hey]], "", %{}, {1, 0}, 4} | ||
| assert parse_token("hello.world") == {:ok, [[:hello, :world]], "", %{}, {1, 0}, 11} | ||
| end | ||
|
|
||
| test "returns error on invalid patial identifier" do | ||
| assert parse_token("hi.0") == | ||
| {:error, "expected alphabetic character or underscore to continue identifier", "0", | ||
| %{}, {1, 0}, 3} | ||
|
|
||
| assert parse_token("Hello.!") == | ||
| {:error, "expected alphabetic character or underscore to continue identifier", "!", | ||
| %{}, {1, 0}, 6} | ||
| end | ||
|
|
||
| test "returns ok on whitespace" do | ||
| assert parse_token(" hi") == {:ok, [], "hi", %{}, {1, 0}, 1} | ||
| assert parse_token("\nhey") == {:ok, [], "hey", %{}, {2, 1}, 1} | ||
| assert parse_token("\thello") == {:ok, [], "hello", %{}, {1, 0}, 1} | ||
| assert parse_token("\vheya") == {:ok, [], "heya", %{}, {1, 0}, 1} | ||
| assert parse_token("\rhiya") == {:ok, [], "hiya", %{}, {1, 0}, 1} | ||
| assert parse_token("\fyo") == {:ok, [], "yo", %{}, {1, 0}, 1} | ||
| end | ||
| end | ||
| end |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Do you think we'll have multiple parsecs in here? Otherwise, just
Thrift.Parser.Lexer(lib/thrift/parser/lexer.ex) seems better.