Upgrade to Pro — share decks privately, control downloads, hide ads and more …

JavaCCとSpring Expression Languageを用いたユーザ定義項目 / ...

karabish
February 18, 2021

JavaCCとSpring Expression Languageを用いたユーザ定義項目 / rakus-meetup-20210217

karabish

February 18, 2021
Tweet

More Decks by karabish

Other Decks in Technology

Transcript

  1. !3

  2. #RAKUSMeetup Ϣʔβఆ߲ٛ໨Ͱॏཁͱߟ͑ͨ2ͭͷ͜ͱ ⋅ ܭࢉࣜΛͲ͏ධՁ͢Δͷ͔ − ࣜͱͯ͠ͷਖ਼͠͞ΛධՁ͠ͳ͍ͱ͍͚ͳ͍ − ૊ΊΔܭࢉ͕ࣜ૿͑ͨ৔߹ʹ֦ுͰ͖Δඞཁ͕͋Δ ➢ JavaCC

    ⋅ Ͳ͏΍ͬͯܭࢉ݁ՌΛࢉग़͢Δͷ͔ − ܭࢉ݁Ռ͸೔ʑมΘΔ − ܭࢉ݁ՌΛग़͢ʹ͸தؒܭࢉ͕Ͱ͖Δඞཁ͕͋Δ ➢ SpEL(Spring Expression Language) !7
  3. #RAKUSMeetup 1 + 1͕ೖྗ͞ΕͨΒʮ2ʯΛग़ྗ͢Δαϯϓϧ PARSER_BEGIN(Example) public class Example { public

    static void main(String args[]) { Example parser = new Example(System.in); parser.Start(); } } PARSER_END(Example) SKIP: { " " } TOKEN: { <NUMBER: (["0"-"9"])> | <OPERATOR: "+" > | <EOL: "\n" >} void Start(): { int sum; int number; } { sum = number() ( <OPERATOR> {} number = number() { sum += number; } )* <EOL> { System.out.println("> " + sum); } } int number(): { Token number; } { number = <NUMBER> {} { return Integer.parseInt(number.image); } } !15
  4. #RAKUSMeetup 1 + 1͕ೖྗ͞ΕͨΒʮ2ʯΛग़ྗ͢Δαϯϓϧ $ javacc example.jj $ ls -l

    -rw-r--r-- 1 user group 6290 1 2 12:34 Example.java -rw-r--r-- 1 user group 457 1 2 12:34 ExampleConstants.java -rw-r--r-- 1 user group 7228 1 2 12:34 ExampleTokenManager.java -rw-r--r-- 1 user group 6171 1 2 12:34 ParseException.java -rw-r--r-- 1 user group 12351 1 2 12:34 SimpleCharStream.java -rw-r--r-- 1 user group 4058 1 2 12:34 Token.java -rw-r--r-- 1 user group 4420 1 2 12:34 TokenMgrError.java -rw-r--r-- 1 user group 375 1 2 12:34 example.jj !16
  5. #RAKUSMeetup numberม਺ͷ஋ + 2000ͷܭࢉ݁ՌΛऔಘ͢Δαϯϓϧ class Data { public int number

    = 21; } ExpressionParser parser = new SpelExpressionParser(); EvaluationContext context = new StandardEvaluationContext(new Data()); Expression exp = parser.parseExpression("number + 2000"); // year = 2021 int year = exp.getValue(context, Integer.class); !19