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

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

Bc0167df60f90a38e0ec30895b7ecc6c?s=47 karabish
February 18, 2021

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

Bc0167df60f90a38e0ec30895b7ecc6c?s=128

karabish

February 18, 2021
Tweet

Transcript

  1. JavaCCͱSpring Expression Language Λ༻͍ͨϢʔβఆ߲ٛ໨ Yuya Yamaguchi@rakus

  2. #RAKUSMeetup Profile ⋅ SIerʹ5೥ࡏ੶ͨ͠ͷͪɺϥΫεʹೖࣾ ⋅ ָָۈଵνʔϜʹॴଐ ⋅ Backend Engineer +

    CI !2
  3. !3

  4. #RAKUSMeetup ͓࿩͢͠Δ͜ͱ ⋅ Ϣʔβఆ߲ٛ໨ͱ͸ ⋅ Ϣʔβఆ߲ٛ໨ΛͲ͏΍࣮ͬͯݱͨ͠ͷ͔ !4

  5. #RAKUSMeetup Ϣʔβఆ߲ٛ໨ͱ͸ ⋅ ۈ຿৘ใΛར༻ͨ͠೚ҙͷܭࢉࣜΛ߲࣋ͬͨ ໨ΛఆٛͰ͖Δػೳ ⋅ ܭࢉ݁ՌΛۈ຿৘ใ͔Βࢀর ⋅ اۀಠࣗͷ༻ޠ΍࢒ۀ࣌ؒͷࢉग़ͳͲΛ૝ఆ !5

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

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

    ⋅ Ͳ͏΍ͬͯܭࢉ݁ՌΛࢉग़͢Δͷ͔ − ܭࢉ݁Ռ͸೔ʑมΘΔ − ܭࢉ݁ՌΛग़͢ʹ͸தؒܭࢉ͕Ͱ͖Δඞཁ͕͋Δ ➢ SpEL(Spring Expression Language) !7
  8. #RAKUSMeetup ܭࢉࣜͷධՁͱܭࢉ݁Ռͷࢉग़ JavaCC SpEL JSON !8

  9. #RAKUSMeetup ܭࢉࣜͷධՁͱܭࢉ݁Ռͷࢉग़ JavaCC SpEL JSON !9

  10. #RAKUSMeetup ܭࢉࣜͷධՁͱܭࢉ݁Ռͷࢉग़ JavaCC SpEL ܭࢉࣜ !10

  11. #RAKUSMeetup ܭࢉࣜͷධՁͱܭࢉ݁Ռͷࢉग़ JavaCC SpEL !11

  12. #RAKUSMeetup ܭࢉࣜͷධՁͱܭࢉ݁Ռͷࢉग़ JavaCC SpEL ܭࢉࣜͱม਺ !12

  13. #RAKUSMeetup ܭࢉࣜͷධՁͱܭࢉ݁Ռͷࢉग़ JavaCC SpEL ܭࢉࣜ !13

  14. #RAKUSMeetup JavaCCͱ͸ ⋅ Java Compiler Compiler − https://javacc.github.io/javacc/ ⋅ BSD

    License 2.0 ⋅ ֦ுBNF͔ΒJavaͷύʔαΛੜ੒ !14
  15. #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
  16. #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
  17. #RAKUSMeetup ܭࢉࣜͷධՁͱܭࢉ݁Ռͷࢉग़ JavaCC SpEL ܭࢉࣜͱม਺ !17

  18. #RAKUSMeetup SpEL(Spring Expression Language)ͱ͸ ⋅ JSPͷELࣜΛSpringʹ֦ு − ${name} ⋅ Spring

    3.0͔Βಋೖ ⋅ @ValueΞϊςʔγϣϯͰར༻ !18
  19. #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
  20. #RAKUSMeetup ·ͱΊ ⋅ JavaCCͰܭࢉࣜΛධՁ͠ɺSpELͰܭࢉ݁Ռͷࢉग़͠·ͨ͠ ⋅ JavaCCΛར༻͢Δ͜ͱͰܭࢉࣜΛಠཱͤ͞Δ͜ͱͰ֦ுੑ͕ Ͱ͖ͨ ⋅ JavaCCͷݺͼग़͠ίετ͕ߴ͍໰୊͕վળ՝୊Ͱ͢ !20