flow2schema/src/collector/pragmas.js

36 lines
635 B
JavaScript
Raw Normal View History

2018-07-25 08:38:10 +03:00
// @flow
2017-12-01 22:39:03 +03:00
import {invariant} from '../utils';
2017-11-28 16:35:28 +03:00
2017-12-01 22:39:03 +03:00
import type {Type} from '../types';
2018-07-24 23:13:34 +03:00
import {createNumber, isRepr} from '../types';
2017-11-28 16:35:28 +03:00
export type Pragma =
2018-07-24 23:13:34 +03:00
| TypePragma
;
2017-11-28 16:35:28 +03:00
export type TypePragma = {
kind: 'type',
value: Type,
};
const PRAGMA_RE = /^\s*@repr\s+\{\s*(.+?)\s*\}\s*$/gm;
export function extractPragmas(text: string): Pragma[] {
const pragmas = [];
let match;
while ((match = PRAGMA_RE.exec(text))) {
const repr = match[1];
2018-07-24 23:13:34 +03:00
invariant(isRepr(repr));
2017-11-28 16:35:28 +03:00
2017-12-01 20:00:40 +03:00
pragmas.push({
2017-11-28 16:35:28 +03:00
kind: 'type',
2018-07-24 23:13:34 +03:00
value: createNumber(repr),
2017-12-01 20:00:40 +03:00
});
2017-11-28 16:35:28 +03:00
}
return pragmas;
}