Skip to content

Commit

Permalink
Permit comma separators between text literals, docs, and names, allow…
Browse files Browse the repository at this point in the history
…ing line breaks for text.
  • Loading branch information
amyjko committed Jul 13, 2024
1 parent 0708bfb commit 5874b86
Show file tree
Hide file tree
Showing 18 changed files with 120 additions and 102 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ Dates are in `YYYY-MM-DD` format and versions are in [semantic versioning](http:
- Hide login link on landing page if logged in.
- Fixed closing text delimiter localization.
- Hide comma separator when localizing names and docs.
- Permit comma separators between text literals, docs, and names, allowing line breaks for text.

## 0.10.4 2024-07-08

Expand Down
2 changes: 1 addition & 1 deletion src/components/editor/DocView.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
<span class="doc"
><NodeView node={node.open} /><NodeView node={node.markup} /><NodeView
node={node.close}
/><NodeView node={node.language} /></span
/><NodeView node={node.language} /><NodeView node={node.separator} /></span
>

<style>
Expand Down
2 changes: 1 addition & 1 deletion src/components/editor/FormattedTranslationView.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
<span class="doc"
><NodeView node={node.open} /><NodeView node={node.markup} /><NodeView
node={node.close}
/><NodeView node={node.language} /></span
/><NodeView node={node.language} /><NodeView node={node.separator} /></span
>

<style>
Expand Down
5 changes: 3 additions & 2 deletions src/components/editor/NameView.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,6 @@
export let node: Alias;
</script>

<NodeView node={node.separator} /><strong><NodeView node={node.name} /></strong
><NodeView node={node.language} />
<strong><NodeView node={node.name} /></strong><NodeView
node={node.language}
/><NodeView node={node.separator} />
2 changes: 1 addition & 1 deletion src/components/editor/TranslationView.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,4 @@

<NodeView node={node.open} /><NodeSequenceView nodes={node.segments} /><NodeView
node={node.close}
/><NodeView node={node.language} />
/><NodeView node={node.language} /><NodeView node={node.separator} />
9 changes: 2 additions & 7 deletions src/components/project/RootView.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
} from './Contexts';
import Root from '@nodes/Root';
import Source from '@nodes/Source';
import Name from '@nodes/Name';
import Program from '@nodes/Program';
import { locales } from '../../db/Database';
import TextLiteral from '../../nodes/TextLiteral';
Expand Down Expand Up @@ -120,12 +119,8 @@
else {
if (nameDocOrText.language)
newHidden.add(nameDocOrText.language);
// Hide the separator.
if (
!priorVisible &&
nameDocOrText instanceof Name &&
nameDocOrText.separator
)
// Hide the separator, if there is one.
if (!priorVisible && nameDocOrText.separator)
newHidden.add(nameDocOrText.separator);
priorVisible = true;
}
Expand Down
8 changes: 7 additions & 1 deletion src/nodes/Doc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,19 +21,22 @@ export default class Doc extends LanguageTagged {
readonly markup: Markup;
readonly close: Token | undefined;
readonly language?: Language;
readonly separator: Token | undefined;

constructor(
open: Token,
markup: Markup,
close: Token | undefined,
lang: Language | undefined,
lang: Language | undefined = undefined,
separator: Token | undefined = undefined,
) {
super();

this.open = open;
this.markup = markup;
this.close = close;
this.language = lang;
this.separator = separator;

this.computeChildren();
}
Expand All @@ -44,6 +47,7 @@ export default class Doc extends LanguageTagged {
new Markup(content ?? []),
new Token(DOCS_SYMBOL, Sym.Doc),
lang,
undefined,
);
}

Expand All @@ -61,6 +65,7 @@ export default class Doc extends LanguageTagged {
{ name: 'markup', kind: node(Markup) },
{ name: 'close', kind: node(Sym.Doc) },
{ name: 'language', kind: optional(node(Language)) },
{ name: 'separator', kind: optional(node(Sym.Separator)) },
];
}

Expand All @@ -70,6 +75,7 @@ export default class Doc extends LanguageTagged {
this.replaceChild('markup', this.markup, replace),
this.replaceChild('close', this.close, replace),
this.replaceChild('language', this.language, replace),
this.replaceChild('separator', this.separator, replace),
) as this;
}

Expand Down
9 changes: 8 additions & 1 deletion src/nodes/FormattedTranslation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,20 +20,23 @@ export default class FormattedTranslation extends LanguageTagged {
readonly open: Token;
readonly markup: Markup;
readonly close: Token | undefined;
readonly language?: Language;
readonly language: Language | undefined;
readonly separator: Token | undefined;

constructor(
open: Token,
markup: Markup,
close: Token | undefined,
lang: Language | undefined,
separator: Token | undefined,
) {
super();

this.open = open;
this.markup = markup;
this.close = close;
this.language = lang;
this.separator = separator;

this.computeChildren();
}
Expand All @@ -44,6 +47,7 @@ export default class FormattedTranslation extends LanguageTagged {
new Markup(content ?? []),
new Token(FORMATTED_SYMBOL, Sym.Formatted),
language,
undefined,
);
}

Expand All @@ -69,6 +73,7 @@ export default class FormattedTranslation extends LanguageTagged {
{ name: 'markup', kind: node(Markup) },
{ name: 'close', kind: node(Sym.Formatted) },
{ name: 'language', kind: optional(node(Language)) },
{ name: 'separator', kind: optional(node(Sym.Separator)) },
];
}

Expand All @@ -78,6 +83,7 @@ export default class FormattedTranslation extends LanguageTagged {
this.replaceChild('markup', this.markup, replace),
this.replaceChild('close', this.close, replace),
this.replaceChild('language', this.language, replace),
this.replaceChild('separator', this.separator, replace),
) as this;
}

Expand All @@ -91,6 +97,7 @@ export default class FormattedTranslation extends LanguageTagged {
this.markup,
this.close,
language,
this.separator,
);
}

Expand Down
47 changes: 22 additions & 25 deletions src/nodes/Name.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,24 +17,24 @@ import type Locales from '../locale/Locales';
import type LanguageCode from '@locale/LanguageCode';

export default class Name extends LanguageTagged {
readonly name: Token;
readonly separator: Token | undefined;
readonly name: Token | undefined;

constructor(
separator: Token | undefined,
name: Token | undefined,
language?: Language,
name: Token,
language: Language | undefined = undefined,
separator: Token | undefined = undefined,
) {
super(language);

this.separator = separator;
this.name = name;
this.separator = separator;

this.computeChildren();
}

static make(name?: string, lang?: Language) {
return new Name(undefined, new NameToken(name ?? '_'), lang);
return new Name(new NameToken(name ?? '_'), lang, undefined);
}

getDescriptor() {
Expand All @@ -43,22 +43,22 @@ export default class Name extends LanguageTagged {

getGrammar(): Grammar {
return [
{ name: 'separator', kind: optional(node(Sym.Separator)) },
{ name: 'name', kind: optional(node(Sym.Name)) },
{ name: 'name', kind: node(Sym.Name) },
{ name: 'language', kind: optional(node(Language)) },
{ name: 'separator', kind: optional(node(Sym.Separator)) },
];
}

clone(replace?: Replacement) {
return new Name(
this.replaceChild('separator', this.separator, replace),
this.replaceChild('name', this.name, replace),
this.replaceChild('language', this.language, replace),
this.replaceChild('separator', this.separator, replace),
) as this;
}

simplify() {
return new Name(this.separator, this.name, undefined);
return new Name(this.name, undefined, this.separator);
}

getCorrespondingDefinition(context: Context): Definition | undefined {
Expand Down Expand Up @@ -95,40 +95,37 @@ export default class Name extends LanguageTagged {
return this.separator !== undefined
? this
: new Name(
new Token(COMMA_SYMBOL, Sym.Separator),
this.name,
this.language,
new Token(COMMA_SYMBOL, Sym.Separator),
);
}

/** Symbolic if it matches the binary op regex */
isSymbolic() {
return (
this.name !== undefined &&
(this.name.text.getLength() === 1 ||
this.name.text
.getText()
.split('')
.every((c) => ReservedSymbols.includes(c)))
this.name.text.getLength() === 1 ||
this.name.text
.getText()
.split('')
.every((c) => ReservedSymbols.includes(c))
);
}

getName(): string | undefined {
return this.name instanceof Token
? this.name.text.toString()
: this.name;
getName(): string {
return this.name.getText();
}

withName(name: string) {
return new Name(this.separator, new NameToken(name), this.language);
return new Name(new NameToken(name), this.language, this.separator);
}

startsWith(prefix: string) {
return this.name && this.name.startsWith(prefix);
}

withoutLanguage() {
return new Name(this.separator, this.name, undefined);
return new Name(this.name, undefined, this.separator);
}

getLowerCaseName(): string | undefined {
Expand Down Expand Up @@ -158,12 +155,12 @@ export default class Name extends LanguageTagged {
}

getDescriptionInputs() {
return [this.name?.getText()];
return [this.name.getText()];
}

getGlyphs() {
return {
symbols: this.name?.getText() ?? '',
symbols: this.name.getText(),
emotion: Emotion.kind,
};
}
Expand Down
11 changes: 7 additions & 4 deletions src/nodes/Names.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ export default class Names extends Node {

// Add name separators if lacking
this.names = names.map((name, index) =>
index > 0 && name.separator === undefined
index < names.length - 1 && name.separator === undefined
? name.withSeparator()
: name,
);
Expand All @@ -31,15 +31,18 @@ export default class Names extends Node {

static make(names: string[] = []) {
const list: Name[] = [];
let first = true;
let count = 0;
for (const name of names) {
count++;
list.push(
new Name(
first ? undefined : new Token(COMMA_SYMBOL, Sym.Separator),
new NameToken(name),
undefined,
count === names.length
? undefined
: new Token(COMMA_SYMBOL, Sym.Separator),
),
);
first = false;
}
return new Names(list);
}
Expand Down
8 changes: 7 additions & 1 deletion src/nodes/Translation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,18 +21,21 @@ export default class Translation extends LanguageTagged {
readonly open: Token;
readonly segments: TranslationSegment[];
readonly close: Token | undefined;
readonly separator: Token | undefined;

constructor(
open: Token,
segments: TranslationSegment[],
close: Token | undefined,
language?: Language,
language: Language | undefined = undefined,
separator: Token | undefined = undefined,
) {
super(language);

this.open = open;
this.segments = segments;
this.close = close;
this.separator = separator;

/** Unescape the text string */

Expand All @@ -45,6 +48,7 @@ export default class Translation extends LanguageTagged {
[new Token(text ?? '', Sym.Words)],
new Token("'", Sym.Text),
language,
undefined,
);
}

Expand All @@ -61,6 +65,7 @@ export default class Translation extends LanguageTagged {
},
{ name: 'close', kind: node(Sym.Text) },
{ name: 'language', kind: optional(node(Language)) },
{ name: 'separator', kind: optional(node(Sym.Separator)) },
];
}

Expand All @@ -70,6 +75,7 @@ export default class Translation extends LanguageTagged {
this.replaceChild('segments', this.segments, replace),
this.replaceChild('close', this.close, replace),
this.replaceChild('language', this.language, replace),
this.replaceChild('separator', this.separator, replace),
) as this;
}

Expand Down
6 changes: 3 additions & 3 deletions src/parser/Tokens.ts
Original file line number Diff line number Diff line change
Expand Up @@ -190,7 +190,7 @@ export default class Tokens {

if (!this.hasNext()) return nodes;
// If there are more tokens, and and it's not the end of a code block in markup, and we're either on the first token, or the next token has a line break, read another token.
this.untilDo(
this.whileDo(
() =>
this.hasNext() &&
(onFirst === true ||
Expand All @@ -210,7 +210,7 @@ export default class Tokens {
* If the action returns false, we stop.
* If the action doesn't consume a token, we stop, to prevent infinite loops.
**/
untilDo(condition: () => boolean, action: () => unknown) {
whileDo(condition: () => boolean, action: () => unknown) {
while (condition()) {
const currentToken = this.peek();
if (action() === false) break;
Expand All @@ -223,7 +223,7 @@ export default class Tokens {
* Completes an action, then checks a condition, and stops if false, otherwise repeats. If the action returns false, we stop.
* If the action doesn't consume a token, we stop, to prevent infinite loops.
**/
doUntil(action: () => unknown, condition: () => boolean) {
doWhile(action: () => unknown, condition: () => boolean) {
do {
const currentToken = this.peek();
if (action() === false) break;
Expand Down
Loading

0 comments on commit 5874b86

Please sign in to comment.