microsoft/TypeAgent
Publicmirrored from https://github.com/microsoft/TypeAgentAvailable
ts/extensions/agr-language/sample.agr
101lines · modecode
| 1 | // Copyright (c) Microsoft Corporation. |
| 2 | // Licensed under the MIT License. |
| 3 | |
| 4 | // Sample Action Grammar file demonstrating syntax highlighting |
| 5 | // This file shows all the major syntax elements |
| 6 | |
| 7 | // Import syntax |
| 8 | @import { Cardinal, TrackName, ArtistName } from "./base.agr" |
| 9 | @import * from "./shared.agr" |
| 10 | |
| 11 | // Simple rule definition |
| 12 | @ <Start> = <Greeting> | <Command> |
| 13 | |
| 14 | // Rule with captures and types |
| 15 | @ <Greeting> = |
| 16 | hello $(name:PersonName) -> { |
| 17 | actionName: "greet", |
| 18 | parameters: { |
| 19 | name |
| 20 | } |
| 21 | } |
| 22 | | hi there -> { actionName: "greet" } |
| 23 | |
| 24 | // Rule with optional elements |
| 25 | @ <Command> = |
| 26 | play (the)? $(track:TrackName) |
| 27 | by $(artist:ArtistName) -> { |
| 28 | actionName: "playTrack", |
| 29 | parameters: { |
| 30 | trackName: track, |
| 31 | artists: [artist] |
| 32 | } |
| 33 | } |
| 34 | | pause (the)? music? -> { actionName: "pause" } |
| 35 | |
| 36 | // Rule with multiple patterns |
| 37 | @ <Volume> = |
| 38 | (turn | set) (the)? volume to $(level:number) -> { |
| 39 | actionName: "setVolume", |
| 40 | parameters: { level } |
| 41 | } |
| 42 | | volume up -> { actionName: "volumeUp" } |
| 43 | | volume down -> { actionName: "volumeDown" } |
| 44 | |
| 45 | // Bare -> results (non-object): string literal, number, or variable reference |
| 46 | @ <PromptSpec> = ( |
| 47 | ('ask me' | 'confirm' | 'please confirm') -> "true" | |
| 48 | ("don't ask" | 'without asking' | 'skip') -> "false" |
| 49 | ) |
| 50 | |
| 51 | @ <FilterSpec> = ( |
| 52 | 'by query' $(userQuery:string) -> userQuery | |
| 53 | 'by category' $(category:string) -> category | |
| 54 | 'all' -> "all" |
| 55 | ) |
| 56 | |
| 57 | // Numeric patterns |
| 58 | @ <Cardinal> = |
| 59 | $(x:number) |
| 60 | | one -> 1 |
| 61 | | two -> 2 |
| 62 | | three -> 3 |
| 63 | | four -> 4 |
| 64 | | five -> 5 |
| 65 | |
| 66 | // String type references |
| 67 | @ <TrackName> = $(x:string) |
| 68 | @ <ArtistName> = $(x:string) |
| 69 | @ <PersonName> = $(x:string) |
| 70 | |
| 71 | // Complex nested rule |
| 72 | @ <PlayCommand> = |
| 73 | <PlayTrack> | <PlayAlbum> | <PlayArtist> |
| 74 | |
| 75 | @ <PlayTrack> = |
| 76 | play track $(n:<Cardinal>) -> { |
| 77 | actionName: "playTrackNumber", |
| 78 | parameters: { |
| 79 | trackNumber: n, |
| 80 | source: "current" |
| 81 | } |
| 82 | } |
| 83 | |
| 84 | @ <PlayAlbum> = |
| 85 | play (the)? album $(album:<AlbumName>) -> { |
| 86 | actionName: "playAlbum", |
| 87 | parameters: { albumName: album } |
| 88 | } |
| 89 | |
| 90 | @ <PlayArtist> = |
| 91 | play music by $(artist:<ArtistName>) -> { |
| 92 | actionName: "playArtist", |
| 93 | parameters: { artistName: artist } |
| 94 | } |
| 95 | |
| 96 | @ <AlbumName> = $(x:string) |
| 97 | |
| 98 | // Operators demonstration |
| 99 | @ <Operators> = |
| 100 | one? two* three+ four // optional, zero-or-more, one-or-more |
| 101 | | (first | second | third) // alternation with grouping |