GithubHelp home page GithubHelp logo

Comments (3)

fuhrmanator avatar fuhrmanator commented on August 16, 2024 1

More banging on PEG.js reveals a pretty powerful parser for these cases:

/* test for CAP DSL */
/* currently parsing works, but no results generated */ 
/* Javascript goes inside the {} */

Expression
  = Spacing Rule+ EndOfFile

Rule
/* define specific rules for the activity types */
  = head:ExamActivity _ tail:Timing Spacing {
      return head + ':' + tail;
  }
    / activity:MoodleQuizActivity _ opens:MoodleQuizOpenTime _ closes:MoodleQuizCloseTime Spacing {
      return activity + ':\n opens:' + opens + ',\n closes: ' + closes;
    }
    / activity:MoodleHomeworkActivity _ opens:MoodleHomeworkAllowSubmissionsTime _ due:MoodleHomeworkDueTime _ cutoff:MoodleHomeworkCutoffTime Spacing {
      return activity + ':\n allow submissions after:' + opens + ',\n due date: ' + due + ',\n cutoff date: ' + cutoff;
    }

ExamActivity
  = head:EXAM_ACTIVITY_CODE tail:Integer {
    return head + tail;
  }
MoodleQuizActivity 
  = head:MOODLE_QUIZ_ACTIVITY_CODE tail:Integer {
    return head + tail;
  }
MoodleQuizOpenTime "Moodle Quiz Open Time" = Timing  
MoodleQuizCloseTime "Moodle Quiz Close Time" = Timing  

MoodleHomeworkActivity 
  = head:MOODLE_HOMEWORK_ACTIVITY_CODE tail:Integer {
    return head + tail;
  }
MoodleHomeworkAllowSubmissionsTime "Moodle Homework Allow Submissions Time" = Timing  
MoodleHomeworkDueTime "Moodle Homework Due Time" = Timing  
MoodleHomeworkCutoffTime "Moodle Homework Cutoff Time" = Timing  




Activity "Activity Number (e.g., E1 for Exam 1)"
 = head:ActivityCode tail:Integer {
     return head + tail;
   }

Timing
/* Case for Session, Labs, Practica */
  = head:MeetingSequence tail:TimeModifier? {
    var result = head, i;
    if (tail !== null) {
      for (i=0; i<tail.length; i++) {
        result += tail[i];
      }
    }
    return result;
  }

ActivityCode 
  = code:(EXAM_ACTIVITY_CODE / MOODLE_QUIZ_ACTIVITY_CODE / MOODLE_HOMEWORK_ACTIVITY_CODE) { return code; }

MeetingSequence "Meeting Number (e.g., S2 for Seminar 2)"
  = meeting:(SEMINAR_MEETING / LABORATORY_MEETING / PRACTICUM_MEETING) number:Integer { return meeting + ' ' + number}

TimeModifier
  = time:(MEETING_START / MEETING_END) adjust:((('-'/'+') DeltaTime) ('@' HHMM)?)?

DeltaTime 
  = Integer ('m' / 'h' / 'd' / 'w') 

/* http://stackoverflow.com/a/20123018/1168342 -- except the order is different in PEG (first match) and there's a bug with the ? between [0-1]?[0-9] */ 
HHMM
  = ([2][0-3] / [0-1]?[0-9] / [0-9]) ':' [0-5][0-9] { return text() }

Integer "integer"
  = [0-9]+ { return parseInt(text(), 10); }

_ "whitespace"
  = [ \t]*

EXAM_ACTIVITY_CODE 
  = EXAM_ACTIVITY_CODE:'E' { return "Exam "}
MOODLE_QUIZ_ACTIVITY_CODE 
  = 'Q' { return "Moodle Quiz "}
MOODLE_HOMEWORK_ACTIVITY_CODE 
  = 'H' { return "Moodle Homework "}

SEMINAR_MEETING 
  = 'S' {return 'Seminar'; }
LABORATORY_MEETING
  = 'L' {return 'Laboratory'; }
PRACTICUM_MEETING
  = 'P' {return 'Practicum'; }

MEETING_START 
  = 'S' {return '(start)'; }
MEETING_END 
  = 'F' {return '(end)'; }

Spacing 
  = (Space / Comment)*
Comment 
  = '#' (!EndOfLine .)* EndOfLine { return 'comment';}
Space 
  = ' ' / '\t' / EndOfLine
EndOfLine 
  = '\r\n' / '\n' / '\r'
EndOfFile 
  = !. { return "EOF"; }

Here's the sample it parses:

# here's a comment
E1 S2
Q1 S1F S2S-30m
H1 L2F L3S-1d@23:55 L3S-1d@23:55

and the resulting output:

[
   [
      "comment"
   ],
   [
      "Exam 1:Seminar 2",
      "Moodle Quiz 1:
 opens:Seminar 1(end)null,
 closes: Seminar 2(start)-,30,m,",
      "Moodle Homework 1:
 allow submissions after:Laboratory 2(end)null,
 due date: Laboratory 3(start)-,1,d,@,23:55,
 cutoff date: Laboratory 3(start)-,1,d,@,23:55"
   ],
   "EOF"
]

Also, error messages are somewhat automatic. For example, leaving off the 3rd date for the H1 line (it requires 3 dates), I get the following error message:

Line 4, column 21: Expected Moodle Homework Cutoff Time but end of input found.

from course-activity-planner.

fuhrmanator avatar fuhrmanator commented on August 16, 2024

As for python solutions with PEG, the documentation for https://github.com/erikrose/parsimonious looks good.

from course-activity-planner.

fuhrmanator avatar fuhrmanator commented on August 16, 2024

Good reference for PEG is the research paper : http://www.brynosaurus.com/pub/lang/peg.pdf

from course-activity-planner.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.